Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the significance of @javax.persistence.Lob annotation in JPA?

When should I use @javax.persistence.Lob annotation in JPA? What datatypes can be annotated by this annotation?

like image 322
Dev Avatar asked Apr 08 '15 09:04

Dev


People also ask

What is javax persistence lob?

javax.persistenceSpecifies 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.

What is lob in Spring JPA?

<< Back to JPA Tutorial The @Lob annotation is used to specify that the currently annotated entity attribute represents a large object type. LOB or Large OBject refers to a variable-length datatype for storing large objects. The datatype has two variants: CLOB – Character Large Object will store large text data.

What is javax persistence used for?

persistence. Java Persistence is the API for the management for persistence and object/relational mapping. Used with the Access annotation to specify an access type to be applied to an entity class, mapped superclass, or embeddable class, or to a specific attribute of such a class.

What is the meaning of persistence in JPA?

Data Persistence is a means for an application to persist and retrieve information from a non-volatile storage system. Persistence is vital to enterprise applications because of the required access to relational databases.


2 Answers

@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 binarry 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.

like image 69
Zielu Avatar answered Oct 21 '22 15:10

Zielu


According to: https://docs.oracle.com/javaee/7/api/javax/persistence/Lob.html

@Lob Specifies that a persistent property or field should be persisted as a large object to a database-supported large object type.

@javax.persistence.Lob signifies that the annotated field should be represented as BLOB (binary data) in the DataBase.

I suppose in database it could be not only binary data but character-based. As we could have BLOB and CLOB. Here's examples in java code:

@Lob
@Column(name = "CHARS", columnDefinition = "CLOB")
private String chars;`

@Lob
@Basic(fetch = FetchType.LAZY)
@Column(name = "DATA", columnDefinition = "BLOB", nullable = false)
private byte[] data;
like image 23
levrun Avatar answered Oct 21 '22 15:10

levrun