21 Temmuz 2023 Cuma

RegionFactory Arayüzü - Pluggable Caching Provider

Giriş
Açıklaması şöyle
org.hibernate.cache.spi.RegionFactory defines the integration between Hibernate and a pluggable caching provider. hibernate.cache.region.factory_class is used to declare the provider to use.

4 Mayıs 2023 Perşembe

@Batch Anotasyonu

Giriş
JPQL ile gelen LEFT JOIN FETCH kullanmak istemiyorsak @Batch kullanılabilir

Örnek
Şöyle yaparız.
@Entity
public class Doctor {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String name;

  @OneToMany(mappedBy = "doctor", fetch = FetchType.LAZY)
  @org.hibernate.annotations.BatchSize(size = 5)
  private Collection<Appointment> appointments;
}
Açıklaması şöyle. Tek bir Doctor nesnesi çeksek bile biraz daha fazla veri çekiyor. Aslında bu bence kötü bir çözüm.
Whenever Hibernate initializes one Doctor – Appointments collection it will also initialize Appointments for four more Doctors. The query which Hibernate generates is as follows.

SELECT appointmen0_.doctor_id,
 appointmen0_.id,
 appointmen0_.id, 
 appointmen0_.appointmentTime, 
 appointmen0_.doctor_id 
FROM Appointment 
 WHERE appointmen0_.doctor_id IN (?, ?, ?, ?, ?)

In our example of 10 doctors, a ceiling of N + 1 / @BatchSize, which is 10 + 1 / 5 = 3 total queries will be executed to fetch Doctors and all the Appointments. It’s not really a great optimization as still more queries are executed than we would like, but it is some improvement.

Please also bear in mind that it’s a global optimization. Once this annotation is present, referring to one of our Doctor’s appointments will result preloading Appointments for another four Doctors.

1 Aralık 2022 Perşembe

MetadataBuilderContributor Arayüzü - Custom SQL Metodu İçindir

Giriş
Açıklaması şöyle
Since Hibernate 5.2.18, you can use the MetadataBuilderContributor utility to customize the MetadataBuilder even if you are bootstrapping via JPA.
Örnek
Şöyle yaparız
import org.hibernate.boot.MetadataBuilder;
import org.hibernate.boot.spi.MetadataBuilderContributor; import org.hibernate.dialect.function.SQLFunctionTemplate; import org.hibernate.type.BooleanType; public class SQLFunctionContributor implements MetadataBuilderContributor { @Override public void contribute(MetadataBuilder metadataBuilder) { metadataBuilder.applySqlFunction("json_contains_key", new SQLFunctionTemplate(BooleanType.INSTANCE, "JSON_CONTAINS(?1, JSON_QUOTE(?2))")); } }
Açıklaması şöyle
In the above snippet, I am overriding the contribute method by providing one of the MySQL JSON functions.

The JSON_CONTAINS() function checks whether one JSON document contains another JSON document.

JSON_CONTAINS(target_json, candidate_json)

Parameters

target_jsonRequired. A JSON document.

candidate_jsonRequired. The included JSON document.

As you see, I have created a custom key “json_contains_key”. This actually will be used in Criteria API or Query DSL and will be parsed by Hibernate as JSON_CONTAINS().

And provide the custom MetadataBuilderContributor to Hibernate via the hibernate.metadata_builder_contributor configuration property.

spring.jpa.properties.hibernate.metadata_builder_contributor=com.abhicodes.customdialect.util.SQLFunctionContributor



8 Kasım 2022 Salı

PostDeleteEventListener Arayüzü

Giriş
Şu satırı dahil ederiz
import org.hibernate.event.spi.PostDeleteEventListener;
Örnek
Şöyle yaparız
public class HibernateCDCEventListener implements PostDeleteEventListener,
PostUpdateEventListener, PostInsertEventListener { public HibernateCDCEventListener(EntityManagerFactory entityManagerFactory) { SessionFactoryImplementor sessionFactory = entityManagerFactory .unwrap(SessionFactoryImplementor.class); EventListenerRegistry eventListenerRegistry = sessionFactory.getServiceRegistry() .getService(EventListenerRegistry.class); eventListenerRegistry.appendListeners(POST_DELETE, this); eventListenerRegistry.appendListeners(POST_UPDATE, this); eventListenerRegistry.appendListeners(POST_INSERT, this); } @Override public void onPostDelete(final PostDeleteEvent event) { // Handle delete events } @Override public void onPostInsert(final PostInsertEvent event) { processEntity(event.getEntity(), event.getEntity(), event.getEntity()); } @Override public void onPostUpdate(final PostUpdateEvent event) { processEntity(event.getEntity(), event.getEntity(), event.getEntity()); } @Override public boolean requiresPostCommitHanding(final EntityPersister persister) { return false; } }

7 Haziran 2022 Salı

HibernateSearch @NaturalId Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.hibernate.annotations.NaturalId;
Örnek
Şöyle yaparız
@Indexed
@Entity
@Table
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class Plant {
  
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @FullTextField()
  @NaturalId
  private String name;

  @FullTextField
  @NaturalId
  private String scientificName;

  @FullTextField
  private String family;

  private Instant createdAt ;
}


18 Nisan 2022 Pazartesi

HibernateSearch @FullTextFieldAnotasyonu

Giriş
Şu satırı dahil ederiz.
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
Açıklaması şöyle
we annotate the fields we want to search on with the @FullTextField annotation. This annotation works only for string fields, but others exist for fields of different types.
Örnek
Şöyle yaparız
import org.hibernate.annotations.NaturalId;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;


@Indexed
@Entity
@Table(name = "plant")
public class Plant {

  @FullTextField
  @NaturalId
  private String name;

  @FullTextField
  @NaturalId
  private String scientificName;

  @FullTextField
  private String family;
  
}



7 Kasım 2021 Pazar

@Length Anotasyonu - Bean Validation

Giriş
Şu satırı dahil ederiz. Bean validation için kullanılır
import org.hibernate.validator.constraints.Length;
Örnek
Şöyle yaparız
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotNull;

public record EnterpriseRecord(String id, 
  @NotNull String name, 
  @NotNull @Length(min = 2, max = 255) String address) {
}