36

How is a blob column annotated in Hibernate? So far I have a class that has:

@Column( name = "FILEIMAGE" )
private byte[ ] fileimage ;
//
public byte[ ] getFileimage ( ) { return this.fileimage ; }
public void setFilename ( String filename ) { this.filename = filename ; }

2 Answers2

53

@Lob should do the trick for blob and clob (use String as type)

@Column( name = "FILEIMAGE" )
@Lob(type = LobType.BLOB)
private byte[] fileimage;
HeDinges
  • 4,367
  • 4
  • 30
  • 28
  • 47
    dependent on the hibernate version, the Lob annotation could have no type parameter. quote from [here](https://www.hibernate.org/398.html): @Lob no longer has attributes, the lob type (CLOB, BLOB) is guessed. If the underlying type is a String or an array of character then CLOB are used. Othersise BLOB are used. – Fortega Jun 02 '09 at 14:04
  • thanks guys for your quick answers. is the sequence important? @Column( name = "FILEIMAGE" , length = 1048576 ) @Lob private byte[ ] fileimage ; –  Jun 02 '09 at 14:24
  • I have not a parameters for @Lob in hibernate 5.1. – Menai Ala Eddine - Aladdin Mar 27 '18 at 17:35
3

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:

  1. Table in db, the column still defined in BLOB; change the hibernate mapping from type="blob" to type="binary"
  2. In Java getter/setter, using byte[] instead of BLOB (javax.sql)
  3. Change in the Java code which get and set this column properly. If involving InputStram, use byte[] to read/write to BLOB column; If reading from DB by using java.sql.ResultSet, make sure use getBytes() instead of getBlob() method.
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
Mingwei Wu
  • 31
  • 1