我想使用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";
}

任何建议都是宝贵的.

推荐答案

当我创建表时,"name"列恰好是第一列.那不好.

Postgresql相关问答推荐

在Go中,如何在没有数据库包的情况下运行PostgreSQL查询?

为MCV扩展统计设置统计目标

时间戳的postgreSQL-to_char如果为零,则不显示微秒

为什么我的唯一索引在 Postgresql 中不起作用?

在连接字符串并将其附加到 PostgreSQL 中的 where 子句时出现语法错误.怎么修?

ST_Intersects 太慢

如何在 kubernetes 中安全地重启 postgresql 容器?

Postgres 转储按顺序插入

Cloud SQL 时间点 数据驻留

带有初始数据的 docker postgres 不会在提交中持久化

是否可以在 PostgreSQL 中部分刷新materialized视图?

没有查询要创建的in ... error

避免 created_at 和 updated_at 由 sequelize 自动生成

如何正确索引多对多关联表?

你如何在postgresql中做mysqldump?

在 Postgresql 中拆分逗号分隔的字段并对所有结果表执行 UNION ALL

如何增加 max_locks_per_transaction

每个数据库提供程序类型允许的最大参数数是多少?

在 postgresql 中获取上个月的数据

在 postgres 中导出为 CSV 并使用 GZIP 压缩