类型为long的错误值: - Postgresql,Hibernate,Spring [英] bad value for type long: - Postgresql, Hibernate, Spring

查看:101
本文介绍了类型为long的错误值: - Postgresql,Hibernate,Spring的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Spring MVC和Hibernate
在PostgresQL中存储一个实体(一个String +一个图像)这是我的表。

I wanna store an entity(a String + an image) in PostgresQL using Spring MVC and Hibernate Here is my table. The image is supposed to be the type of 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
);

这是我想存储的实体。

Here is the entity that I want to store.

    @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

您可以看到变量名称是一个字符串,不长。仍然当我提交表单的值不是数字时,它会抛出 org.postgresql.util.PSQLException:long类型的值不正确:x

You can see the variable "name" is a String, not Long. Still when I submit the form with a value which is not numeric it throws org.postgresql.util.PSQLException: Bad value for type long : x

这里是表单:

here is the form:

<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而导致的,但我不知道应该如何消除此异常。其实我也不明白excpetion的名字。它表示类型 long 的值不合适。但谁想要长型? 变量name是String类型!!!!

If I enter a numeric value and submit it, OK. But any non-numeric value triggers the above mentioned exception...I read that it might be caused by that I do not use OID properly but I do not know what should I do to eliminate this exception. Actually I do not understand the name of the excpetion either. It says "bad value for type long" . but who wants type long? the variable "name" is type String!!!!

最后,这里是Controller

Finally, here is the Controller

@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";
}

任何建议都是appriciated。

Any advice is appriciated.

推荐答案

当我创建表时,名称列碰巧是第一个。这不好。
Id必须是第一列。如果我改变列的顺序,它可以正常工作......

when I created the table the column "name" happened to be the first. That's not good. Id must be the first column. If I change the order of columns it works fine...

这篇关于类型为long的错误值: - Postgresql,Hibernate,Spring的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆