0

I'm using struts2 and hibernate and I want to know how to store and retrieve images from database using hibernate annotations in the POJO class

Moe
  • 270
  • 5
  • 12

3 Answers3

2

Best way to store images in the database in in format of Byte arraylike you have to upload images using struts2 file upload utility and den pass it on to hibernate as byte[] image;

in your mapping you have to do something like

@Column( name = "IMAGE" )
@Lob(type = LobType.BLOB)
private byte[] image;

How to use annotation for this is very well described in the following thread

proper hibernate annotation for byte[]

Community
  • 1
  • 1
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204
0

Or simply:

@Lob
private byte[] picture;
yglodt
  • 13,807
  • 14
  • 91
  • 127
  • In my case, this doesn't works, Gives error - `Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: wrong column type encountered in column [image] in table [productlines]; found [mediumblob (Types#LONGVARBINARY)], but expecting [tinyblob (Types#VARBINARY)]` – PAA Dec 26 '19 at 06:00
0

The answer is as following

private byte[] imageBefore;

@Type(type="org.hibernate.type.BinaryType")
@Column (name = "IMAGE_BEFORE") 
public byte[] getImageBefore() {
    return imageBefore;
}

refer to this link if you're not using annotations and for a complete reference

Moe
  • 270
  • 5
  • 12