San*_*ejű 14 java postgresql spring hibernate blob
我想使用Spring MVC和Hibernate在PostgresQL中存储一个实体(一个String +一个图像)这是我的表.该图像应该是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
);
Run Code Online (Sandbox Code Playgroud)
这是我想要存储的实体.
@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
Run Code Online (Sandbox Code Playgroud)
您可以看到变量"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>
Run Code Online (Sandbox Code Playgroud)
如果我输入一个数值并提交,那就OK.但是任何非数字值都会触发上面提到的异常......我读到它可能是因为我没有正确使用OID但我不知道该怎么做才能消除这个异常.其实我也不明白这个激动的名字.它说"类型长的坏价值".但谁想要打字?变量"name"是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";
}
Run Code Online (Sandbox Code Playgroud)
任何建议都是适当的.
Tše*_*ele 48
我有一个类似的问题,但它与数据库中ID字段的顺序无关.
经过一些搜索后,我发现这指向了Hibernate中的Lobs被视为OID的事实,除非另有说明.
这意味着Hibernate将尝试将Lob放入Long a因此产生异常PSQLException:类型为long的值不正确
指定Lob是否被视为文本的方法是通过注释字段
@Lob
@Type(type = "org.hibernate.type.TextType")
Run Code Online (Sandbox Code Playgroud)
起初我尝试设置
@Column(columnDefinition = "text")
Run Code Online (Sandbox Code Playgroud)
而不是 @Lob 注释,正如这里已经提到的。它在 PostgreSQL 上对我有用,但出现错误 org.hibernate.tool.schema.spi.SchemaManagementException:架构验证:表 [问题] 中的列 [htmlText] 中遇到错误的列类型;找到了 [clob (Types#CLOB)],但期望 [text (Types#VARCHAR)]
在我的单元测试中(在 HSQLDB 上)。
然后它尝试了
@Column(columnDefinition = "clob")
Run Code Online (Sandbox Code Playgroud)
它在 PostgreSQL 和 HSQLDB 上都运行良好!
| 归档时间: |
|
| 查看次数: |
22300 次 |
| 最近记录: |