Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad value for type long: - Postgresql, Hibernate, Spring

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 

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> 

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!!!!

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

Any advice is appriciated.

like image 548
Sanyifejű Avatar asked Sep 28 '12 21:09

Sanyifejű


1 Answers

I had a similiar problem but it was not related to the order of ID field in the database.

After some searching I found this pointing to the fact that Lobs in Hibernate are treated as OIDs unless otherwise specified.

That means Hibernate will try put a Lob into a Long a hence produce that exception PSQLException: Bad value for type long

The way to specify that the Lob is a to be treated as text is by annotating the field

@Lob @Type(type = "org.hibernate.type.TextType") 
like image 168
Tšeliso Molukanele Avatar answered Oct 20 '22 00:10

Tšeliso Molukanele