【发布时间】:2012-09-20 19:05:58
【问题描述】:
我想使用 Spring MVC 和 Hibernate 在 PostgresQL 中存储一个实体(一个字符串 + 一个图像) 这是我的桌子。图片应该是oid的类型。
CREATE TABLE document
(
name character varying(200),
id serial NOT NULL,
content oid, // that should be the image
CONSTRAINT document_pkey PRIMARY KEY (id )
)
WITH (
OIDS=FALSE
);
这是我要存储的实体。
@Entity
@Table(name = "document")
public class Document {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
@Column(name="content")
private Blob content; //this is the image
//getters- setters
你可以看到变量“name”是一个String,而不是Long。仍然当我提交带有非数字值的表单时,它会抛出 org.postgresql.util.PSQLException: Bad value for type long : x
这里是表格:
<form:form method="post" action="save.html" commandName="document" enctype="multipart/form-data">
<form:errors path="*" cssClass="error"/>
<table>
<tr>
<td><form:label path="name">Name</form:label></td>
<td><form:input path="name" /></td>
</tr>
<tr>
<td><form:label path="content">Document</form:label></td>
<td><input type="file" name="file" id="file"></input></td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Add Document"/>
</td>
</tr>
</table>
</form:form>
如果我输入一个数值并提交,OK。但是任何非数字值都会触发上述异常......我读到这可能是由于我没有正确使用 OID 造成的,但我不知道应该怎么做才能消除这个异常。其实我也不明白 excpetion 的名字。它说“类型 long 的价值不高”。但是谁想要长字呢? 变量“name”是字符串类型!!!
最后是控制器
@RequestMapping(value = "/save", method = RequestMethod.POST)
public String save(@ModelAttribute("document") Document document, @RequestParam("file") MultipartFile file) {
try {
Blob blob = Hibernate.createBlob(file.getInputStream());
document.setContent(blob);
documentDao.save(document);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/index.html";
}
任何建议都会被采纳。
【问题讨论】:
-
您可能想尝试使用 hibernate @Lob 注释来注释您的 Blob 声明。此外,打开 hibernate 的查询输出可能会有所帮助,这样您就可以看到正在生成的 sql 并查看它是否会提示您发送到数据库的内容。
-
OID 不能保存 BLOB。 "The oid type is currently implemented as an unsigned four-byte integer."。我想这就是错误的来源。
-
@madth3
oid用于保存对pg_largeobject表中的大对象的引用。 -
当然,很抱歉。你检查this question了吗?您使用的是哪个版本的 PostgreSQL?
-
您使用的是哪个版本的 PostgreSQL? PostgreSQL 9.1
标签: java spring hibernate postgresql blob