22 Mayıs 2018 Salı

hibernate.cfg.xml Dosyası - hbm2ddl Ayarları

auto Alanı
Şu değerler olabilir.
create
update
validate

1. create Seçeneği
Tüm tabloları drop eder ve tekrar yaratır.

2. update Seçeneği
Bu seçenek yeni sütun ekler ancak mevcut sütuna dokunmaz. İsmini değiştirmez.

Örnek
Şöyle yaparız.
<property name="hibernate.hbm2ddl.auto">update</property>
3. validate seçeneği
Örnek ver

import_files_sql_extractor Alanı
Şöyle yaparız.
<property name="hibernate.hbm2ddl.import_files_sql_extractor"
  value="org.hibernate.tool.hbm2ddl.MultipleLinesSqlCommandExtractor" />

21 Mayıs 2018 Pazartesi

HibernateSearch @IndexedEmbedded Anotasyonu

Giriş
Açıklaması şöyle.
This annotation is used to index associated entities (@ManyToMany, @*ToOne, @Embedded and @ElementCollection) as part of the owning entity. This is needed since a Lucene index document is a flat data structure which does not know anything about object relations.
Örnek
Şöyle yaparız.
public class Eventinfo implements Serializable {

  @IndexedEmbedded
  private Set<EventLicenseType> eventLicenceTypeIds;
  ...
}

15 Mayıs 2018 Salı

@Cache Anotasyonu

Giriş
Şu satırı dahil ederiz. İkinci seviye ön bellek içindir.
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
Şu değerleri alabilir
- ReadOnlyCache
- ReadWriteCache
- NonstrictReadWriteCache
- TransactionalCache

Açıklaması şöyle
Read-only
Useful for data that is read frequently but never updated (e.g. referential data like Countries). It is simple. It has the best performances of all (obviously).

Read/write
Desirable if your data needs to be updated. But it doesn't provide a SERIALIZABLE isolation level, phantom reads can occur (you may see at the end of a transaction something that wasn't there at the start). It has more overhead than read-only.

Nonstrict read/write
Alternatively, if it's unlikely two separate transaction threads could update the same object, you may use the nonstrict–read–write strategy. It has less overhead than read-write. This one is useful for data that are rarely updated.

Transactional
If you need a fully transactional cache. Only suitable in a JTA environment.

NONSTRICT_READ_WRITE
Örnek
Şöyle yaparız.
@Entity
@Table(...)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class MyAnswer implements Serializable {
  ...
}

@OnDelete Anotasyonu

Giriş
OneToMany veya OneToOne ilişkide kullanılır.

Örnek
Şöyle yaparız.
@OneToMany(fetch = FetchType.EAGER, mappedBy = "parent")
@OnDelete(action = OnDeleteAction.CASCADE) 
private List<Tree> children; 
Örnek
Şöyle yaparız. Burada parent child ilişkisi tam olarak yok. Ancak door silinirse house da silinir.
@OneToOne
@OnDelete(action = OnDeleteAction.CASCADE)
Object door;

@SelectBeforeUpdate Anotasyonu

Giriş
@DynamicUpdate anotasyonuna da bakılabilir.

value = true
Açıklaması şöyle.
creates a select before update to know which properties has been changed this is useful when the entity has been loaded and updated on different sessions.
Örnek
Şöyle yaparız.
@SelectBeforeUpdate(value=true)

@DynamicUpdate Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.hibernate.annotations.DynamicUpdate;
@SelectBeforeUpdate anotasyonuna da bakılabilir.
Açıklaması şöyle
By default, Hibernate issues an UPDATE statement for all entity fields, so the entire row is updated. Doesn’t Hibernate’s @DynamicUpdate annotation solve the problem? It will only update the fields which were changed, right? Well, no. Hibernate will compare the state of the in-memory entity with the database. Then it will generate an UPDATE statement for all columns which are different, regardless of where they changed. 

value = true
Açıklaması şöyle.
dynamic-update (optional - defaults to false): specifies that UPDATE SQL should be generated at runtime and can contain only those columns whose values have changed.
Eğer true ise sadece değişen alanları güncelleyen SQL cümlesini kullanır. Performans açısından iyi olmayabilir çünkü nesne için yeni bir SQL cümlesi yaratılır.

Örnek
Şöyle yaparız.
@DynamicUpdate(value=true)

14 Mayıs 2018 Pazartesi

AnnotationConfiguration Sınıfı - Deprecated

Giriş
Şu satırı dahil ederiz.
import org.hibernate.cfg.AnnotationConfiguration;
Bu sınıf yerine Configuration sınıfını kullanmak lazım.

Configuration sınıfı bir ara bölündü ve bu sınıf ortaya çıktı daha sonra tüm işlev yeniden Configuration sınıfında toplandı.

configure metodu
Şöyle yaparız.
AnnotationConfiguration().configure().buildSessionFactory();
configure metodu - String
Şöyle yaparız.
sessionFactory = new AnnotationConfiguration()
 .configure("/hib.cfg.xml")alo.
 .buildSessionFactory();