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

本秂侑毒 提交于 2019-12-17 18:27:51

问题


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


回答1:


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




回答2:


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;


来源:https://stackoverflow.com/questions/29511133/what-is-the-significance-of-javax-persistence-lob-annotation-in-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!