How to store image into postgres database using hibernate

六眼飞鱼酱① 提交于 2019-12-01 05:26:23

问题


I want to store image into database using hibernate and Java. I am using postgres database

I tried bytea data type to store image and byte[] data type in hibernate pojo.

I used the following code,

CREATE TABLE photo
(
  "photo_name" bytea
)
WITH (OIDS=FALSE);
ALTER TABLE photo OWNER TO postgres;

Hibernate Pojo

public class PhotoEntity {

byte[] name;

public byte[] getName() {
    return name;
}

public void setName(byte[] name) {
    this.name = name;
}

}

but it gives error at time of mapping.
please give me any reference to do this.


回答1:


If you are using Hibernate via JPA2, you may need the @Lob annotation, though I'm not sure if that's for oid or bytea fields. See:

proper hibernate annotation for byte[]

There's also a Hibernate dev blog post that's quite informative.

If you're using Hibernate via XML mappings or its own annotations dialect, please show your exact code and error message(s).

See also the answers here.



来源:https://stackoverflow.com/questions/10671471/how-to-store-image-into-postgres-database-using-hibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!