bcosynot
bcosynot

Reputation: 6013

Hibernate: Could not deserialize - invalid stream header

Any ideas as to how I could resolve this error? I am using Spring JPA with Hibernate. Necessary details below.

Entity class 1:

@Entity
@Table(name = "ways")
@TypeDef(name = "hstore", typeClass = HstoreUserType.class)
@Cacheable
public class Way {

    /**
     * Primary key for the row in table.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    /**
     * The ID to represent it across the system.
     * Used for preserving historical information. 
     */
    @Column(name = "way_id")
    private Long wayId;

    /**
     * The version of the way this Object represents. 
     */
    @Column(name = "version")
    private Integer version;


    /**
     * The {@link User} that edited this version. 
     */
    @OneToOne
    @PrimaryKeyJoinColumn(name = "user_id")
    private User user;

    /**
     * Timestamp when this version of the Way was edited.
     */
    @Column(name = "tstamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp;

    /**
     * The changeset that this version of the way belongs to. 
     */
    @Column(name = "changeset_id")
    private Long changesetId;

    /**
     * All the tags this Way contains. 
     */
    @Type(type = "hstore")
    @Column(name = "tags", columnDefinition = "hstore")
    private Object2ObjectOpenHashMap<String, String> tags = new Object2ObjectOpenHashMap<String, String>(); 

    @Column(name = "bbox")
    private Geometry bbox;

    @Column(name = "linestring")
    private Geometry linestring;

    @Column(name = "nodes")
    private Long[] nodes;

    // getters and setters
}

Entity class for User:

@Entity
@Table(name = "users")
@Cacheable
public class User {

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

    @Column(name = "name", unique = true)
    private String name;

    // getters and setters

}

And the stacktrace :

The stacktrace is quite huge. I am pasting the whole stacktrace below for reference and a quick TL;DR here:

javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
Caused by: org.hibernate.type.SerializationException: could not deserialize
Caused by: java.io.StreamCorruptedException: invalid stream header: 30313033

Full stacktrace follows:

javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not deserialize
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1361)
        at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1289)
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:261)
        at org.hibernate.ejb.criteria.CriteriaQueryCompiler$3.getResultList(CriteriaQueryCompiler.java:260)
        at org.springframework.data.jpa.repository.support.SimpleJpaRepository.findAll(SimpleJpaRepository.java:250)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFac
        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySuppo
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor$LockModePopulatingMethodIntercceptor.invoke(LockModeReava:92)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:90)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy36.findAll(Unknown Source)
        at com.osmrecommend.persistence.service.WayPersistenceServiceImpl.getAllWays(WayPersistenceServiceImpl.java:32)
        at com.osmrecommend.dao.WayDAO.getAllWays(WayDAO.java:37)
        at com.osmrecommend.cbf.TFIDFModelBuilder.get(TFIDFModelBuilder.java:90)
        at com.osmrecommend.cbf.TFIDFModelBuilder.get(TFIDFModelBuilder.java:36)
        at org.grouplens.grapht.util.MemoizingProvider.get(MemoizingProvider.java:59)
        at org.grouplens.lenskit.inject.StaticInjector.instantiate(StaticInjector.java:130)
        at org.grouplens.lenskit.inject.StaticInjector.apply(StaticInjector.java:137)
        at org.grouplens.lenskit.inject.StaticInjector.apply(StaticInjector.java:47)
        at org.grouplens.lenskit.eval.traintest.ComponentCache$NodeInstantiator.call(ComponentCache.java:166)
        at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4792)
        at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3599)
        at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2379)
        at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2342)
        at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2257)
        at com.google.common.cache.LocalCache.get(LocalCache.java:4000)
        at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4789)
        at org.grouplens.lenskit.eval.traintest.ComponentCache$Instantiator.apply(ComponentCache.java:126)
        at org.grouplens.lenskit.eval.traintest.ComponentCache$Instantiator.apply(ComponentCache.java:104)
        at org.grouplens.lenskit.inject.RecommenderInstantiator$1.apply(RecommenderInstantiator.java:109)
        at org.grouplens.lenskit.inject.RecommenderInstantiator$1.apply(RecommenderInstantiator.java:99)
        at org.grouplens.lenskit.inject.RecommenderInstantiator.replaceShareableNodes(RecommenderInstantiator.java:188)
        at org.grouplens.lenskit.inject.RecommenderInstantiator.instantiate(RecommenderInstantiator.java:99)
        at org.grouplens.lenskit.eval.traintest.LenskitEvalJob.buildRecommender(LenskitEvalJob.java:74)
        at org.grouplens.lenskit.eval.traintest.TrainTestJob.runEvaluation(TrainTestJob.java:117)
        at org.grouplens.lenskit.eval.traintest.TrainTestJob.call(TrainTestJob.java:101)
        at org.grouplens.lenskit.eval.traintest.JobGraph$JobNode.call(JobGraph.java:116)
        at org.grouplens.lenskit.eval.traintest.JobGraph$JobNode.call(JobGraph.java:102)
        at org.grouplens.lenskit.util.parallel.SequentialTaskGraphExecutor.execute(SequentialTaskGraphExecutor.java:37)
        at org.grouplens.lenskit.eval.traintest.TrainTestEvalTask.runEvaluations(TrainTestEvalTask.java:468)
        at org.grouplens.lenskit.eval.traintest.TrainTestEvalTask.perform(TrainTestEvalTask.java:398)
        at org.grouplens.lenskit.eval.traintest.SimpleEvaluator.call(SimpleEvaluator.java:313)
        at com.osmrecommend.app.OSMRecommendEval.main(OSMRecommendEval.java:94)
Caused by: org.hibernate.type.SerializationException: could not deserialize
        at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)
        at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)
        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:131)
        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:117)
        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:39)
        at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:67)
        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:65)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:269)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:265)
        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238)
        at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:357)
        at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2695)
        at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1552)
        at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1484)
        at org.hibernate.loader.Loader.getRow(Loader.java:1384)
        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:640)
        at org.hibernate.loader.Loader.doQuery(Loader.java:856)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289)
        at org.hibernate.loader.Loader.doList(Loader.java:2463)
        at org.hibernate.loader.Loader.doList(Loader.java:2449)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2279)
        at org.hibernate.loader.Loader.list(Loader.java:2274)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:470)
        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:355)
        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:196)
2014-04-13 17:35:49 INFO  persistence.service.WayPersistenceServiceImpl:37 - All ways fetched in 331s
        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1115)
        at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:252)
        ... 51 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 30313033
        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)
        at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:328)
        at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:318)
        at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:237)
        ... 78 more

UPDATE:

I updated the Ways entity class to the following, but error still occurs :

@Entity
@Table(name = "ways")
@Cacheable
public class Way implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -7941769011539363185L;

    /**
     * Primary key for the row in table.
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    /**
     * The ID to represent it across the system.
     * Used for preserving historical information. 
     */
    @Column(name = "way_id")
    private Long wayId;

    /**
     * The version of the way this Object represents. 
     */
    @Column(name = "version")
    private Integer version;


    /**
     * The {@link User} that edited this version. 
     */
    @OneToOne
    @PrimaryKeyJoinColumn(name = "user_id")
    private User user;

    /**
     * Timestamp when this version of the Way was edited.
     */
    @Column(name = "tstamp")
    @Temporal(TemporalType.TIMESTAMP)
    private Date timestamp;

    /**
     * The changeset that this version of the way belongs to. 
     */
    @Column(name = "changeset_id")
    private Long changesetId;

    /**
     * All the tags this Way contains. 
     */
    @Column(name = "tags")
    private String tags; 

    @Column(name = "bbox")
    private Geometry bbox;

    @Column(name = "linestring")
    private Geometry linestring;

    @Column(name = "nodes")
    @ElementCollection(targetClass=Long.class)
    private List<Long> nodes;

Upvotes: 54

Views: 141136

Answers (18)

irina
irina

Reputation: 1

I encountered the same exception once.

I had accidentally imported the timestamp field as java.security.Timestamp instead of the right one for this case, java.sql.Timestamp. I didn't notice that at the beginning.

Upvotes: 0

David Victor
David Victor

Reputation: 827

Just another outlier if you end up stuck with this one. In my case the IDE kindly auto-imported java.security.Timestamp instead of java.sql.Timestamp - the latter being serializable.

The backtrace as per the OP and error below were being seen. Ho hum.

StreamCorruptedException: invalid stream header: 78690512 

Upvotes: 0

alejoriosrivas
alejoriosrivas

Reputation: 1

Brother, this error already happened to me, that happen 'cause the variable types doesn't match with the DB columns data types.

Sample: Your MySQL schema has a row 'age' with (INT) type.

Your code has his identifier 'age' with a (Number) type, it must be (Integer) type to solve the 'Serialization error).

Hoppe you can solve this problem, people :D

Upvotes: 0

acveer
acveer

Reputation: 380

This error can happen due to many reasons or possible scenarios, the best way to isolate is to enable the hibernate LOG level to INFO to source where its originating from.

I encountered this error recently and its because we have custom JSON to String convertor, which is not being applied to an JPA entity property.

Upvotes: 0

Steve Omondi
Steve Omondi

Reputation: 31

I just took 30 of my minutes on this similar problem Caused by: java.io.StreamCorruptedException: invalid stream header: 30313033. This solution posted in hibernate-spatial Nibble helped me http://hibernate-spatial.1140993.n2.nabble.com/exception-disabling-contexual-lob-creation-tp7572477p7572484.html.

Male sure you set spring.jpa.database-platform=org.hibernate.spatial.dialect.postgis.PostgisDialect In place of ##spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect

Usually if this is the culprit you'll find out in the Hibernate logs INFO Level

Look for HHH000400: It should be Using dialect: org.hibernate.spatial.dialect.postgis.PostgisDialect for correct setting.

Upvotes: 1

user7563540
user7563540

Reputation:

For others with the same problem where none of the previous answers helped to solve the issue:

TLDR: Check your dependencies / referenced libraries for duplicates.

The mentioned error message can also show up when a project already has some older (or same) version of the vividsolutions/locationtech JTS library included, and then later on hibernate-spatial was added to the project (which ships with its own JTS library dependency). This can be especially hard to figure out, when hibernate-spatial resides in a different (for example osgi) module and thus no compiler warnings show the different library versions being used in different parts of your code.

The Sample code of the thread starter references object type "Geometry", thus the JTS (or geolatte) library was very likely used, which also means that hibernate-spatial was most likely used.

Upvotes: 0

MavidDeyers
MavidDeyers

Reputation: 290

I had a similar problem with the exception, but the cause was slightly different for me. In the POJO generation, an Oracle data type "TIMESTAMP (6) WITH TIME ZONE" became an object of type Serializable. Since this attribute was not relevant for me, I just removed it.

Upvotes: 1

Nimesh
Nimesh

Reputation: 41

This type of issue related to hibernate-spatial dependency to the project or related to hibernate dialect in the case of geometry (in Spring Boot it is spring.jpa.database-platform property)? It should help.

Upvotes: 1

Curtis Yallop
Curtis Yallop

Reputation: 7329

Your entity mapping annotations (or xml) do not match your db table. So hydration/deserialization failed.

So which column is the problem? It does not tell you directly.

One clue is the hex codes at the end eg invalid stream header: 53....

53 happens to be the ascii code for "S". (https://ascii.cl/)

You can get the hex like this: select hex(MyColumn) from MyTable;

So you can look at the hex of suspicious columns until you find it.

Also, you can remove the columns one-by-one from the entity-mapping (eg comment out @Column and replace it with @Transient) until it works. Start with the suspicious ones eg enums or custom serializers. Or perhaps with any columns you just added that might have made it break.

Upvotes: 3

Benjam&#237;n Valero
Benjam&#237;n Valero

Reputation: 394

The answer from Alexey Simonov almost works for me. However, I am still forced to used Java 7 and I am using the java.time backport from ThreeTen.

In my case, I have just used a different converter suited for the backported classes:

@Column(name = "lastupdate", nullable = false)
@Convert(converter = ThreeTenBackPortJpaConverters.LocalDateConverter.class)
private LocalDate lastUpdate;

Upvotes: 2

Anand Rockzz
Anand Rockzz

Reputation: 6668

I was facing this on a @NamedNativeQuery + @SqlResultSetMapping scenario. The @ConstructorResult's targetClass had a Custom Enum inside. Changing it to String and later converting it in my code resolved the issue.

Upvotes: 9

user8495084
user8495084

Reputation: 21

I faced this issue :

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.type.SerializationException: could not deserialize

Got solution also.

For me db side some rows manually entered the data after trying to retrieve the data raised exception deleted the manually entered rows after that tried to retrieve the data so this time no exception. Means if you insert the data serialized entities through hibernate in db store it. Db stores serialized objects format and retrieve db rows through serialized entities.

Upvotes: 2

Alexey Simonov
Alexey Simonov

Reputation: 425

Regarding the kaba713 answer and converting java.time.* by Hibernate.

I use org.springframework.data.jpa.convert.threeten.Jsr310JpaConverters; and it works with Spring-Boot 1.5.4.

@Column(name = "order_date", nullable = false)
@Convert(converter = Jsr310JpaConverters.LocalDateTimeConverter.class)
private LocalDateTime orderDate;

And we don't need to write a custom converter.

Upvotes: 17

kaba713
kaba713

Reputation: 494

Regarding the answer from @molholm, I would advise against using a deprecated dependency. The specific problem is that earlier versions of Hibernate did not convert java.time.* by them self, so you had to write a converter. The latest version (5.2.1.Final from 06/2016) seems to have an integrated converter. But I would still advise you to write your own and add it to your attribute. I.e.:

@Convert(converter = LocalDateConverter.class)
private LocalDate timestamp;

And here is the converter:

import java.sql.Date;
import java.time.LocalDate;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class LocalDateConverter implements AttributeConverter<LocalDate, Date> {

    @Override
    public Date convertToDatabaseColumn(LocalDate entityValue) {
        if (entityValue == null) {
            return null;
        }
        return Date.valueOf(entityValue);
    }

    @Override
    public LocalDate convertToEntityAttribute(Date databaseValue) {
        if (databaseValue == null) {
            return null;
        }
        return databaseValue.toLocalDate();
    }
}

Upvotes: 9

molholm
molholm

Reputation: 2032

For others with the same error (does not answer this exact question)

I experienced this error with hibernate 4.3 and 5.0.5 due to fields of type java.time.LocalDateTime, I have several entities using LocalDateTime, but only one of them caused the issue, I don't know why.

It was fixed by adding the following dependency:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-java8</artifactId>
    <version>${hibernate.version}</version>
</dependency>

Upvotes: 76

Felipe Cadena
Felipe Cadena

Reputation: 121

The problem should be for the Geometry relation, it is mapped as a regular column so Hibernate try to map a simple Column to a Geometry object. I had a similar problem and the solution was to explicity define the relation, something like:

@OneToOne
@JoinColumn(name = "bbox")
private Geometry bbox;

Upvotes: 9

Surendra Poranki
Surendra Poranki

Reputation: 399

Please make sure your entity objects are implementing the Serializable interface. Please select the option to make your objects Serializable in whatever tool you are using.

Some thing like this

public class Way implements Serializable {
    private static final long serialVersionUID = 1L;

Upvotes: 2

Alexey Malev
Alexey Malev

Reputation: 6531

This issue is happening when something cannot be deserialized (thank you, Captain Obvious). In your entity mappings the following looks suspicious:

@Type(type = "hstore")
@Column(name = "tags", columnDefinition = "hstore")
private Object2ObjectOpenHashMap<String, String> tags = new Object2ObjectOpenHashMap<String, String>(); 

@Column(name = "bbox")
private Geometry bbox;

@Column(name = "linestring")
private Geometry linestring;

My suggestion is either to take a look over Geometry and Object2ObjectHashMap classes - check whether they are serializable itself and all their fields either serializable too, or marked with transient keyword. Also if you post these classes here, this may help aswell. HstoreUserType class listing may also be helpful.

Upvotes: 39

Related Questions