postgresql 如何使用休眠将图像存储到 postgres 数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10671471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to store image into postgres database using hibernate
提问by Dnyani
I want to store image into database using hibernate and Java. I am using postgres database
我想使用休眠和 Java 将图像存储到数据库中。我正在使用 postgres 数据库
I tried bytea
data type to store image and byte[]
data type in hibernate pojo.
我尝试使用bytea
数据类型byte[]
在 hibernate pojo 中存储图像和数据类型。
I used the following code,
我使用了以下代码,
CREATE TABLE photo
(
"photo_name" bytea
)
WITH (OIDS=FALSE);
ALTER TABLE photo OWNER TO postgres;
Hibernate Pojo
休眠 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.
但它在映射时出错。
请给我任何参考来做到这一点。
回答by Craig Ringer
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:
如果您通过 JPA2 使用 Hibernate,您可能需要@Lob
注释,但我不确定它是否用于oid
或bytea
字段。看:
proper hibernate annotation for byte[]
There's also a Hibernate dev blog postthat's quite informative.
还有一篇Hibernate 开发博客文章,内容非常丰富。
If you're using Hibernate via XML mappings or its own annotations dialect, please show your exact code and error message(s).
如果您通过 XML 映射或它自己的注释方言使用 Hibernate,请显示您的确切代码和错误消息。
See also the answers here.
另请参阅此处的答案。