11 Ekim 2018 Perşembe

JPA @Lob Anotasyonu

Giriş
Verinin java.sql.Types.BLOB veya java.sql.Types.CLOB olarak kaydedilmesini sağlar. Açıklaması şöyle.
Specifies that a persistent property or field should be persisted as a large object to a database-supported large object type. Portable applications should use the Lob annotation when mapping to a database Lob type. The Lob annotation may be used in conjunction with the Basic annotation or the ElementCollection annotation when the element collection value is of basic type. A Lob may be either a binary or character type.
The Lob type is inferred from the type of the persistent field or property, and except for string and character-based types defaults to Blob.
Açıklaması şöyle.
@javax.persistence.Lob signifies that the annotated field should be represented as BLOB (binary data) in the DataBase.

You can annotate any serializable data type with this annotation. In JPA, upon persisting (retrieval) the field content will be serialized (deserialized) using standard Java serialization.

Common use of LOB is to annotate a HashMap field inside your Entity to store some of the object properties which are not mapped into DB columns. That way all the unmapped values can be stored in the DB in one column in their binary representation. Of course the price that is paid is that, as they are stored in binary format, they are not searchable using the JPQL/SQL.
Kullandığımız ORM bu tipi gerçek veritabanı sütun tipine çevirir. Ancak veritabanları arasında da farklılıklar var. Örneğin PostgreSQL oid sütun tipini kullanırken, Oracle kendi blob tipini kullanıyor.
annotation                   postgres     oracle
-------------------------------------------------
byte[] + @Lob                oid          blob 

JPA @Lob Anotasyonu - PostgreSQL yazısına tababilirsiniz.

Örnek
Bazen veritabanında kullanılacak sütun tipini özellikle belirtmek gerekir. Şöyle yaparız. CLOB ve BLOB Oracle sütun tipleridir.
@Lob
@Column(name = "CHARS", columnDefinition = "CLOB")
private String chars;`

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "DATA", columnDefinition = "BLOB", nullable = false)
private byte[] data;
Örnek
Şöyle yaparız.
@Basic(fetch = FetchType.LAZY) 
@Lob
private String projectDescription = "";
Örnek
Şöyle yaparız.
@Basic(fetch = FetchType.LAZY)
@Lob
@Column(length=5000)
private byte[]  projectDescription1 =new byte[0];

Hiç yorum yok:

Yorum Gönder