Image not getting stored in postgres
I have stored images in image data type in binary format in MSSQL.Now I have migrated my database from MSSQL to Postgres 9.0 and trying to store image files in bytea field.When I am trying to convert these bytes into image file though I am not getting any error but Image is not getting rendered where as the same java code is working fine with MSSQL.My application is struts hibernate based application.My h开发者_Python百科ibernate dto is like following-
@Entity
@Table(name="Image_Type")
public class ImageType extends AbstractPO
{
private static final long serialVersionUID = 1L;
private Long id;
private Long ownerId;
private Short ownerType;
private String name;
private String description;
private Long typeId;
private byte[] originalImage;
private byte[] thumbNailImage;
private byte[] terminalImage;
private String createdBy;
private Date createdOn;
private String modifiedBy;
private Date modifiedOn;
private String rfu1;
private String rfu2;
private String rfu3;
@Id @GeneratedValue(strategy=AUTO, generator="Image_Type_seq")
@SequenceGenerator(name="Image_Type_seq", sequenceName="IMAGE_TYPE_IMAGE_TYPEID_SEQ")
@Column(name="Image_TypeID", unique=true, nullable=false, precision=10, scale=0)
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
@Column(name="OwnerID", precision=10, scale=0)
public Long getOwnerId() {
return this.ownerId;
}
public void setOwnerId(Long ownerId) {
this.ownerId = ownerId;
}
@Column(name="OwnerType", precision=4, scale=0)
public Short getOwnerType() {
return this.ownerType;
}
public void setOwnerType(Short ownerType) {
this.ownerType = ownerType;
}
@Column(name="Name", length=100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name="Description", length=100)
public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
}
@Column(name="TypeID", precision=10, scale=0)
public Long getTypeId() {
return this.typeId;
}
public void setTypeId(Long typeId) {
this.typeId = typeId;
}
@Column(name="Original_Image")
public byte[] getOriginalImage() {
return this.originalImage;
}
public void setOriginalImage(byte[] originalImage) {
this.originalImage = originalImage;
}
@Column(name="ThumbNail_Image")
public byte[] getThumbNailImage() {
return this.thumbNailImage;
}
public void setThumbNailImage(byte[] thumbNailImage) {
this.thumbNailImage = thumbNailImage;
}
@Column(name="Terminal_Image")
public byte[] getTerminalImage() {
return this.terminalImage;
}
public void setTerminalImage(byte[] terminalImage) {
this.terminalImage = terminalImage;
}
@Column(name="CreatedBy", length=50)
public String getCreatedBy() {
return this.createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
@Column(name="CreatedOn", nullable=false, length=23)
public Date getCreatedOn() {
return this.createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
@Column(name="ModifiedBy", length=50)
public String getModifiedBy() {
return this.modifiedBy;
}
public void setModifiedBy(String modifiedBy) {
this.modifiedBy = modifiedBy;
}
@Column(name="ModifiedOn", length=23)
public Date getModifiedOn() {
return this.modifiedOn;
}
public void setModifiedOn(Date modifiedOn) {
this.modifiedOn = modifiedOn;
}
@Column(name="RFU1", length=100)
public String getRfu1() {
return this.rfu1;
}
public void setRfu1(String rfu1) {
this.rfu1 = rfu1;
}
@Column(name="RFU2", length=100)
public String getRfu2() {
return this.rfu2;
}
public void setRfu2(String rfu2) {
this.rfu2 = rfu2;
}
@Column(name="RFU3", length=100)
public String getRfu3() {
return this.rfu3;
}
public void setRfu3(String rfu3) {
this.rfu3 = rfu3;
}
}
Kindly help me to resolve this issue
Which version of Postgresql are you using? Are you using server version 9 and JDBC driver 8.4? (because if so: Hibernate 3.3.2GA improperly loads bytea data from PostgreSQL 9.0 and all type mappings are correct)
What is the actual value being stored in the database? Use psql to check, and compare the first 16 bytes or so with the expected values. Since they are images you would expect them to start with some format magic- "JFIF", "GIF", "PNG", "II"/"MM" etc. You need to be able to determine if the data is being corrupted when being saved, corrupted when being loaded or some other problem. Dump the first few bytes of the image array before saving the object and after loading to compare with the values from psql.
精彩评论