11

I'm using two tables by Hibernate and I dont understand why for particular query I have this problem. I hope someone recognizes the problem.

I have a table user

@Entity
@Table(name="user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private Long idUser;
private Area area;

//...other get and setter

@OneToOne(fetch=FetchType.EAGER)
@JoinColumn(name="idarea")
public Area getArea() {
return area;
}
}

and a table area

@Entity
@Table(name = "area")
public class Area implements Serializable {
private static final long serialVersionUID = 1L;

@Id @GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="idarea")
private Long idArea;

@Column(name="area_name")
private String areaName;

@Column(name="time_start")
private LocalTime timeStart;

//...other get and setter

}

LOGS says:

15:27:28,140  INFO DefaultLoadEventListener:160 - Error performing load command
org.hibernate.type.SerializationException: could not deserialize
at org.hibernate.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)
at org.hibernate.util.SerializationHelper.deserialize(SerializationHelper.java:306)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:130)
at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:116)
at 
....//other lines
org.springframework.orm.hibernate3.HibernateSystemException: could not deserialize; nested exception is org.hibernate.type.SerializationException: could not deserialize
3
  • 1
    why there is no primary key in User entity ?
    – invariant
    Commented Nov 6, 2012 at 14:56
  • Where are annotation of other fields in User entity?
    – axtavt
    Commented Nov 6, 2012 at 15:08
  • @invariant sorry I put into get now change for visualization...
    – Shinigami
    Commented Nov 6, 2012 at 15:08

5 Answers 5

8

You can simply annotate above any field of joda time:

   @Temporal(TemporalType.DATE)
   @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
1
  • Actually, this should be the accepted answer. The hint about mixed field and property access is ok, but not the cause of the problem.
    – Christian
    Commented Jan 14, 2022 at 7:17
7

I would recommend to set the annotations only on the fields or the getters. I prefer the fields, but thats just my taste.

See The Curious case of Field and Property Access in Hibernate:

Thus either place the annotations on the fields only or on the getters(properties) only. Mixing them and not using @Access will cause abnormal behaviour.

Then if serialization is part of your application I would recommend to generate better serialVersionUID with a tool.

4
  • Are you using Eclipse? Its built in there. Netbeans I think has a similiar feature.
    – Christian
    Commented Nov 7, 2012 at 14:53
  • yes eclipse, It puts private static final long serialVersionUID = 1L; (I dont't make "get" and "setter", is it a problem?)
    – Shinigami
    Commented Nov 9, 2012 at 7:14
  • I got two options: default and generated. Use generated. My other thought is that your class is not serializable because some members are not serializable. If you can create a test case (i.e. at pastebin.com) I could investigate the problem further. But you accepted my answer. Does this mean, that my proposal to use either getter or fields worked out?
    – Christian
    Commented Nov 9, 2012 at 11:10
  • 1
    SOLVED OK thank you for your advice to investigate about the field. In fact I've a org.joda.time.LocalTime for a time field now I change by java.sql.Time and It works. I dont now why but it happened.
    – Shinigami
    Commented Nov 12, 2012 at 8:23
5

I met with the same mistake but finally, I saw there were no errors in the relation. if you believe there is no error in the relation then check your imports there may wrong import for other fields. for example in my code import java.security.Timestamp imported by idea automatically When I changed wrong import to import java.sql.Timestamp; the error was gone.

0
2

Maybe it is a trivial remark, but if you generated your Entity classes with tools like Eclipse JBoss Hibernate plugin, pay attention to the fact that it sets the type of varchar fields as Serializable instead of String.

At runtime you'll get org.hibernate.type.SerializationException: could not deserialize

I lost a lot of time for such a stupid detail.

1
  • I had this issue for a field of type 'hstore' in postgis, JBoss hibernate mapped it to serializable, I changed it to String and its deserializeing correctly Commented Jun 3, 2017 at 22:05
0

I ran into this when I had a persisted class attribute of type Number instead of Integer

Not the answer you're looking for? Browse other questions tagged or ask your own question.