25 Haziran 2018 Pazartesi

UUIDGenerator Sınıfı

Giriş
8-4-4-4-8 formatında IETF RFC 4122 uyumlu sayı üretir.

UUIDHexGenerator Sınıfı - Kullanmayın

Giriş
Açıklaması şöyle. Şuna benzer bir sayı üretir. "2c929c6646f02fda0146f02fdbfa0000"
A UUIDGenerator that returns a string of length 32, This string will consist of only hex digits

21 Haziran 2018 Perşembe

HibernateSearch @AnalyzerDef Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.apache.lucene.analysis.core.LowerCaseFilterFactory;
import org.apache.lucene.analysis.core.WhitespaceTokenizerFactory;

import org.hibernate.search.annotations.AnalyzerDef;
import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;
Örnek
Şöyle yaparız.
@Entity
@Indexed
@Inheritance
@Table(name = "application",
 uniqueConstraints = { @UniqueConstraint(columnNames = "reference") })
@AnalyzerDef(name = "customAnalyser",
 tokenizer = @TokenizerDef(factory = WhitespaceTokenizerFactory.class), filters = {

        @TokenFilterDef(factory = LowerCaseFilterFactory.class), // Lowercase
                                                                    // all
                                                                    // characters
})
public class Application implements Serializable {
  ...
}

HibernateSearch @Field Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.hibernate.search.annotations.Field;
Date,String,Long gibi tiplere kullanılabilir.
index Alanı
Index.YES ise alan searchable olur.

name Alanı
Lucene Document içinde hangi isim ile saklanacağını belirtir.

store Alanı
Store.YES ile alanın orijinal değeri de saklanır

Örnek
Şöyle yaparız.
@Column(name = "reference")
@Analyzer(definition = "customAnalyser")
@Field(index = Index.YES, analyze = Analyze.YES, store = Store.YES)
private String reference;

HibernateSearch @Indexed Anotasyonu

Giriş
Şu satırı dahil ederiz.
import org.hibernate.search.annotations.Indexed;
Açıklaması şöyle
... the @Index annotation indicates to Hibernate Search that we want to index this entity in order to apply search operation on it.

Örnek
Şöyle yaparız
@Entity
@Indexed
@Table(name = "ApplicationTypeCreation")
public class ApplicationTypeCreation extends Application {
  ...
}
Ö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;

import javax.persistence.*;
import java.time.Instant;

@Indexed
@Entity
@Table(name = "plant")
@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;
} 
routingBinder Alanı - Conditional Index İçindir
Routing Bridges ile gerçekleştirilir

Örnek
Şöyle yaparız. Burada indeksleme koşulunu belirtiyoruz. Eğer name ismi rosaceae ise nesne indekslenmez. Burada RoutingBridge arayüzünden kalıtan sınıf nested kullanılıyor ama harici bir sınıf ta olabilir. RoutingBridge nesnesi RoutingBinder ile Hibernate'e tanıtılır. 
public class PlantRoutingBinder implements RoutingBinder {

  private static final String FIELD_TO_USE = "name";
  private static final List<String> FAMILIES_TO_IGNORE = Arrays.asList("rosaceae");

  @Override
  public void bind(RoutingBindingContext context) {
    context.dependencies().use(FIELD_TO_USE);

    context.bridge(Plant.class, new Bridge());
  }

  public static class Bridge implements RoutingBridge<Plant> {
    @Override
    public void route(
      DocumentRoutes routes,
      Object entityIdentifier,
      Plant indexedEntity,
      RoutingBridgeRouteContext context) {
      if (FAMILIES_TO_IGNORE.contains(indexedEntity.getFamily())) {
        routes.notIndexed();
      } else {
        routes.addRoute();
      }
    }

    @Override
    public void previousRoutes(
      DocumentRoutes routes,
      Object entityIdentifier,
      Plant indexedEntity,
      RoutingBridgeRouteContext context) {
        routes.addRoute();
    }
  }
}
ve kullanmak için şöyle yaparız
@Indexed(routingBinder = @RoutingBinderRef(type = PlantRoutingBinder.class))
@Entity
@Table(name = "plant")
@Getter
@Setter
@ToString
@EqualsAndHashCode
public class Plant {
   ...
}