Databases use the data types BLOB (binary large object) and CLOB (character large object) to store large objects, like images and very long texts. JPA and Hibernate provide two kinds of mappings for these types. You can choose if you want to: Materialize the LOB and map it to a byte[] or a String.
Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping. All the metadata is clubbed into the POJO java file along with the code, this helps the user to understand the table structure and POJO simultaneously during the development.
@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.
In order to map a Many-to-Many relationship we'll use the @ManyToMany annotation.
@Lob should do the trick for blob and clob (use String as type)
@Column( name = "FILEIMAGE" )
@Lob(type = LobType.BLOB)
private byte[] fileimage;
I used hibernate 4 in JBoss 7 and Java 7, and found out the BLOB
column in my table doesn't work like what I have for hibernate 2. Fortunately, I solved it by reading other people solutions.
My solution:
type="blob"
to type="binary"
byte[]
instead of BLOB
(javax.sql
)byte[]
to read/write to BLOB
column; If reading from DB by using java.sql.ResultSet
, make sure use getBytes() instead of getBlob()
method.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With