目前,在数据库中,我有下表(PostgreSQL 9.2):

                        jackpot_image

     id       jackpot_id         mime_type        image_data
   serial      integer          varchar(32)         bytea

我有以下实体:
@Entity
@Table(name = "jackpot_image")
public class JackpotImage {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name="jackpot_id")
    private int jackpotId;

    @Column(name="mime_type")
    private String mimeType;

    @Column(name="image_data")
    private byte[] imageData;

    //GET, SET

}

以下是道:
public class JackpotImageDao {

    public JackpotImage selectImage(int imageId){
        String hql = "FROM JackpotImage WHERE id = :imageId";
        Query query = getSession().createQuery(hql);
        query.setInteger("imageId", imageId);
        return (JackpotImage) query.uniqueResult();
    }

    public void save(JackpotImage image){
        getSession().saveOrUpdate(image);
    }
}

问题是,当我试图保存并检索图像时,我会得到不同的字节数组。
JackpotImage image;
JackpotImageDao imageDao;
//Getting image and ImageDAO
imageDao.save(image);
JackpotImage img = imageDao.selectImage(image.getId());

现在,image的二进制数据不同于img的二进制数据。为什么?我该怎么解决?

最佳答案

向字段imageData添加注释@Lob

09-05 21:26