19 Eylül 2018 Çarşamba

hibernate.properties Dosyası - Kullanmayın

Giriş
Bu dosyada Hibernate'in çalışması için gerekli ayarlar bulunur. Ayarlar SessionCache vs gibi şeylerle ilgilidir. Bu dosyanın xml olarak tutulan hali hibernate.cfg.xml dosyasıdır.

Bu dosya kodlarken src/main/resources dizini altına yerleştirilir.

18 Eylül 2018 Salı

Hibernate Hikari

Giriş
Maven'a dependency'leri ekledikten sonra bir ayar yapmaya gerek yok. Hikari varsayılan ayarlar ile açılıyor.

HikariCPConnectionProvider sınıfı org.hibernate.service.spi.Configurable arayüzünü gerçekleştirdiği için Hibernate otomatik olarak bu sınıfı ilklendiriyor.

Örnek
hibernate.properties dosyasında şöyle yaparız.
hibernate.connection.provider_class=
  org.hibernate.hikaricp.internal.HikariCPConnectionProvider

14 Eylül 2018 Cuma

Hibernate 5 ve Java 8 java.time Sınıfları

Giriş
Tablo şöyle. Java 8 ile gelen bu sınıflar JPA 2.2 ile desteklenecek ancal Hibernate JPA 2.2'den önce desteklemeye başladı. Açıklaması şöyle.
Hibernate 5 natively supports the java 8 Date/Time APIs. Just ensure that the hibernate-java8 jar is on your class path.

Java Type               JDBC Type
java.time.Duration                BIGINT
java.time.Instant                TIMESTAMP
java.time.LocalDateTime TIMESTAMP
java.time.LocalDate         DATE
java.time.LocalTime        TIME
java.time.OffsetDateTime     TIMESTAMP
java.time.OffsetTime        TIME
java.time.ZonedDateTime IMESTAMP

12 Eylül 2018 Çarşamba

Second Level Cache

Giriş
Second Level Cache ile veri ehcache, Hazelcast gibi bir cache sağlayıcısı üzerinde saklanır

Java Serialization Kullanır
Açıklaması şöyle.
No Custom serialization - Memory intensive
If you use second level caching you would not be able to use fast serialization frameworks as Kryo and will have to stick to java serializeable which sucks.

On top of this for each entity type you will have a separate region and within each region you will have entry for each key of each entity. In terms of memory efficiency this is inefficient.
Entity'ler Id Alanı İle Saklanır
Açıklaması şöyle.
Using 2nd level cache is easier to setup but it only stores entities by id. There is also a query cache, storing ids returned by a given query. So the 2nd level cache is a two step process that you need to fine tune to get the best performance. When you execute projection queries the 2nd level object cache won't help you, since it only operates on entity load. The main advantage of 2nd level cache is that it's easier to keep it in sync whenever data changes, especially if all your data is persisted by hibernate.



3 Ağustos 2018 Cuma

SQLQuery Arayüzü

Giriş
Native SQL cümlesi çalıştırmamızı sağlar.

addEntity metodu
Şöyle yaparız. Belirtilen nesne tipinden ArrayList döner.
final SQLQuery query = sf.getCurrentSession().createSQLQuery(
  "select\n" +
  "   id, null as store_id, payment_type,\n" +
  "   sum(cost_before_tax) as amount_before_tax, \n" +
  " sum(ticket_count) as count\n" +
  "from settlement_collection_initial_settlement\n" +
  "where\n" +
  "   business_date between :start and :end\n" +
  (storeID != null ? "    and store_id = :store\n" : "") +
  "group by transaction_type, payment_type"
);
query.addEntity(AmountRow.class);
        query.setDate("start", start);
        query.setDate("end", end != null ? end : start);
        if (storeID != null) {
            query.setString("store", new UUIDType().toSQLString(storeID));
        }
        return query.list();
addScalar metodu
Seçilecek sütunları ve döndürülecek tipleri belirtir. Şöyle yaparız.
query.addScalar("ID", StringType.INSTANCE)
list metodu
Şöyle yaparız.
StringBuilder query = new StringBuilder("SELECT x, y, z FROM ...");

SQLQuery query = session.createSQLQuery(query);
List result = query.list();

23 Temmuz 2018 Pazartesi

Validator DefaultGroupSequenceProvider Arayüzü

Giriş
MyBean sınıfının isRequired alanı true ise belli bir validation anotasyonunun çalışması için kullanılır.

getValidationgGroups metodu
Elimizde şöyle bir arayüz olsun.
public interface Special {
}
Şöyle yaparız.
public class BeanSequenceProvider implements DefaultGroupSequenceProvider<MyBean> {
  @Override
  public List<Class<?>> getValidationGroups(final MyBean object) {
    final List<Class<?>> classes = new ArrayList<>();
    classes.add(MyBean.class);
    if (object != null && object.getisRequired() == true) {
      classes.add(Special.class);
    }
    return classes;
  }

}
Şöyle yaparız.
@GroupSequenceProvider(BeanSequenceProvider.class) // needed at class Level
public class MyBean {


  @NotEmpty(groups = Special.class) //check for condition defined in BeanSequenceProvider
  private String mobileNumber;

  private boolean isRequired;


  // Getters and setters        
}