15

When executing a Criteria Query in hibernate, I get the following exception:

javax.persistence.PersistenceException: javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize: could not deserialize

What could be the problem?

PS: although possibly not relevant, my hibernate version is hibernate-4.0.1 final.

V G
  • 18,822
  • 6
  • 51
  • 89

6 Answers6

27

The problem was that a referenced entity had another reference to an entity and the relationship was NOT annotated by any of the @OneToMany-like annotations.

bluish
  • 26,356
  • 27
  • 122
  • 180
V G
  • 18,822
  • 6
  • 51
  • 89
  • WOW, What timing! I spent the past 2 days trying to figure this same thing out. I had a Department Entity, which had a OneToOne and JoinColumn for a Manager attribute to an Employee Entitye, which also had an attribute Department. After seeing your answer (posted the same date as my issue) I realized my Employee Entity did not have the corresponding OneToMany attribute. Thanks!! – jeff Jan 31 '14 at 20:44
  • I am really glad that I can help :) – V G Feb 02 '14 at 18:49
  • 5
    Unfortunately, I'm having this problem as well...somewhere in ~100 entity classes with 7500+ lines of code. The exception is really not helpful in the slightest. =/ – Alan Krueger Jan 06 '15 at 06:24
  • I just wanted to give JPA another try and ran into this. Not only is JPA itself very easy to use wrong, when you do it doesn't help you to fix the problem... – Patrick Cornelissen Oct 01 '16 at 15:58
7

This exception may occur when hibernate obtains data of unexpected type from database query result. For example hibernate expects number but gets string instead.

In such case look for StreamCorruptedException: "invalid stream header": 74657374 exception in stacktrace. The number is hint for you, but you may want to convert it to text with ascii table. 74657374 gives test as string. Which was value of similarly named table column, but with completely different type. So hibernate was querying wrong column which happened to exist just by chance, so the first exception raised was not column does not exist but could not deserialize instead. Hibernate was expecting long but got String instead.

I got in this mess because correct @Column(name="id_user") was ignored and hibernate infered wrong column name user from field name which wasn't idUser but just user with getUser() getter. The annotation was ignored because it was specified on property getter instead of the field, which was expected by hibernate because the entity superclass annotated ID field with @Id, instead of the ID getter, which is what I groundlessly expected.

Vlastimil Ovčáčík
  • 2,799
  • 27
  • 29
2

In my case it was a problem with java.time.LocalDate from Java 8, and I forgot to assign a converter to that specific attribute.

It seems that the newer Versions (5.2.1Final) has a built in converter, so that can help if your are not bound to a specific version.

But checking the Annotations is definitely the way to go, but I would check them all.

kaba713
  • 484
  • 6
  • 17
0

I had the same problem. It was due to an enum in a @SqlResultSetMapping. The solution is here: @ConstructorResult with Enum in JPA 2.1

Edit, due to a comment from @Eugene Mihaylin:
So, I had the following situation:

    @SqlResultSetMapping(
    name = "MyTargetClassMappingName",
    classes = {
            @ConstructorResult(
                    targetClass = MyTargetClassDTO.class,
                    columns = {
                            @ColumnResult(name = "name_from_query1"),
                            @ColumnResult(name = "name_from_query2"),
                            ...
                            @ColumnResult(name = "name_of_problematic_column", type = MyCustomEnum.class),
                    }
            )
    })
    @Entity
    public class MyTargetClassDTO ...

And I needed to change to:

    @SqlResultSetMapping(
    name = "MyTargetClassMappingName",
    classes = {
            @ConstructorResult(
                    targetClass = MyTargetClassDTO.class,
                    columns = {
                            @ColumnResult(name = "name_from_query1"),
                            @ColumnResult(name = "name_from_query2"),
                            ...
                            @ColumnResult(name = "name_of_problematic_column", type = String.class),
                    }
            )
    })
    @Entity
    public class MyTargetClassDTO ...

in order to fix the issue.

Later, in the constructor of the DTO (annotated as @Entity), I am doing: MyCustomEnum.valueOf(...) in order to parse the value of the string and assign it to the specified field of type MyCustomEnum in the DTO.

Martin Patsov
  • 366
  • 5
  • 10
0

In my case it was a problem with java.time.LocalDateTime from Java 8.

Fix with adding dependency:

<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-java8</artifactId>
</dependency>  
-2

I have my entity class UploadFiles having two fields as fid & fname where fid being primary key. I have my query in dao as

StringBuilder queryString = new StringBuilder("from UploadFiles f where f.fid=1");

Query query = (Query) session.createQuery(queryString.toString());          
return query.list();

when it execute query.list(),it gives serialization exception

Ashish Aggarwal
  • 3,018
  • 2
  • 23
  • 46
shahid
  • 5
  • 1
  • 3