25 Mayıs 2018 Cuma

Restrictions Sınıfı

Giriş
Şu satırı dahil ederiz.
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
eq metodu
Örnek
Şöyle yaparız.
return (Number) 
session.createCriteria(Book.class)add(Restrictions.eq("language", "Java))
              .setProjection(Projections.rowCount())
              .uniqueResult();
Örnek
Şöyle yaparız.
final Session session = sessionFactory.getCurrentSession();
final Criteria criteria = session.createCriteria(User.class);
criteria.add(Restrictions.eq("risk", 1));
List list = criteria.list();

23 Mayıs 2018 Çarşamba

IdentifierGenerator Arayüzü

generate metodu
Şöyle yaparız.
public class StringIdGenerator implements IdentifierGenerator {
  final public static String NAME=StringIdGenerator.class.getSimpleName();
  public Serializable generate(SessionImplementor session, Object object)
 throws HibernateException {
    return  UUID.randomUUID().toString().replace("-","");
  }
}

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;