如何使用hibernate将图像存储到postgres数据库中 [英] How to store image into postgres database using hibernate
问题描述
我想用hibernate和Java将图像存储到数据库中。我使用的是postgres数据库
我尝试了 bytea
数据类型来存储图像, byte [ ]
hibernate pojo中的数据类型。
我使用了下面的代码,
CREATE TABLE photo
(
photo_namebytea
)
WITH(OIDS = FALSE);
ALTER TABLE照片OWNER TO postgres;
Hibernate Pojo
public class PhotoEntity {
byte [] name;
public byte [] getName(){
return name;
}
public void setName(byte [] name){
this.name = name;
}
}
但它会在映射时发生错误。
请给我任何参考。
如果您通过JPA2使用Hibernate,您可能需要 @Lob
注释,尽管我不确定这是否适用于 oid
或 bytea
字段。请参阅:
还有一个 Hibernate开发博客文章,这是相当丰富的。
如果您通过XML映射或自己的注释方言使用Hibernate,请显示您的确切代码和错误消息(s)。
另见答案 here 。
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.
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.
这篇关于如何使用hibernate将图像存储到postgres数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!