28 Mayıs 2018 Pazartesi

JPA @Basic Anotasyonu

Giriş
Açıklaması şöyle.
A basic attribute is one where the attribute class is a simple type such as String, Number, Date or a primitive. A basic attribute's value can map directly to the column value in the database.

The types and conversions supported depend on the JPA implementation and database platform. Any basic attribute using a type that does not map directly to a database type can be serialized to a binary database type.

The easiest way to map a basic attribute in JPA is to do nothing. Any attributes that have no other annotations and do not reference other entities will be automatically mapped as basic, and even serialized if not a basic type. The column name for the attribute will be defaulted, named the same as the attribute name, as uppercase.
fetch Alanı
Primitive alanın lazy load edilmesini sağlar. Şöyle yaparız.
@Lob
@Basic(fetch = FetchType.LAZY)
private byte[] avatar;
Lazy çalışması için Hibernate Bytecode enhancement yapmalıdır. Şöyle yaparız.
<plugin>
  <groupId>org.hibernate.orm.tooling</groupId>
  <artifactId>hibernate-enhance-maven-plugin</artifactId>
  <version>${hibernate.version}</version>
  <executions>
    <execution>
      <configuration>
        <failOnError>true</failOnError>
        <enableLazyInitialization>true</enableLazyInitialization>
      </configuration>
      <goals>
        <goal>enhance</goal>
      </goals>
    </execution>
  </executions>
</plugin>
optional Alanı
Açıklaması şöyle. false ise alanın null değer olarak kaydedilmesini engeller.
@Basic(optional) is (should be) checked on runtime by persistence provider before saving to DB. @Column is a column definition in database and is used for schema generation :
Şöyle yaparız.
@Basic(optional = false)
@Column(name = "HABILITADO")
private short habilitado;

Hiç yorum yok:

Yorum Gönder