2

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 hibernate 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

2
  • What's the database column type? Have you compared what's stored in PostgreSQL, what's stored in MSSQL, and what you have in your entity? Commented Mar 29, 2011 at 11:27
  • In MSSQL, column is of type image and in postgres it is if type bytea. In code, as we can see above byte array is used. Commented Mar 29, 2011 at 12:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

8.4 driver and 9.0 server is indeed a problem, if the bytea_output configuration parameter is not adjusted. But it would be better to upgrade the JDBC driver instead. postgresql.org/docs/current/static/…
Yes I am using postgres9.0. I have changed postgresql.conf file, bytea_output from hex to escape. but it didn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.