我想使用Spring MVC和Hibernate在PostgresQL中存储一个实体(字符串+图像)
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"是一个字符串,不长.但当我提交一个非数值的表单时,它会抛出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>
如果我输入一个数值并提交,好的.但任何非数值都会触发上述异常...我读到这可能是因为我没有正确使用OID,但我不知道该如何消除这个异常.事实上,我也不明白这个例外的名字.上面写着"long型的坏值".但是谁想要长字体呢?the variable "name" is type String!!!!
最后,这里是控制器
@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";
}
任何建议都是宝贵的.