long类型的错误值: – Postgresql,Hibernate,Spring

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

这是我想要存储的实体。

  @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

这是表格:

 
Name
Document

如果我输入数值并提交,请确定。 但是任何非数字值都会触发上面提到的exception……我读到它可能是因为我没有正确使用OID但我不知道该怎么办才能消除这个exception。 其实我也不明白这个重复的名字。 它说“类型长的坏价值”。 但谁想要打字? 变量“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"; } 

任何建议都是适当的。

我有一个类似的问题,但它与数据库中ID字段的顺序无关。

经过一些搜索后,我发现这指出了Hibernate中的Lobs被视为OID的事实,除非另有说明。

这意味着Hibernate将尝试将Lob放入Long a因此产生exceptionPSQLException:类型为long的值不正确

指定Lob被视为文本的方法是通过注释字段

 @Lob @Type(type = "org.hibernate.type.TextType") 

当我创建表时,“name”列碰巧是第一个。 这不好。 Id必须是第一列。 如果我更改列的顺序,它工作正常…

起初我试着设置

 @Column(columnDefinition = "text") 

而不是@Lob注释,如这里已经提到的。 它在PostgreSQL上对我有用,但是出现错误org.hibernate.tool.schema.spi.SchemaManagementException:Schema-validation:表[问题]列[htmlText]中遇到错误的列类型; 找到[clob(Types#CLOB)],但期待[text(Types#VARCHAR)]

在我的unit testing中(在HSQLDB上)。

然后它尝试了

 @Column(columnDefinition = "clob") 

它在PostgreSQL和HSQLDB上都运行良好!

我遇到了smiler错误,原因是我在整数数据类型中有一些除整数以外的字符

eg-,111和144-等