当前位置: 首页 > 面试题库 >

休眠:无法反序列化-无效的流头

丌官翰采
2023-03-14
问题内容

关于如何解决此错误的任何想法?我在Hibernate中使用Spring JPA。下面的必要详细信息。

实体类别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
@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

}

和stacktrace:

stacktrace非常大。我将下面的整个stacktrace粘贴以供参考,并在此处粘贴快速TL; DR:

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

完整的堆栈跟踪如下:

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

更新:

我将Ways实体类更新为以下内容,但仍然出现错误:

@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;

问题答案:

当无法反序列化某些内容时,就会发生此问题(谢谢,上尉船长)。在您的实体映射中,以下内容看起来可疑:

@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;

我的建议是查看类Geometry并进行Object2ObjectHashMap检查-检查它们本身是否可序列化, 以及
它们的所有字段是否也可序列化或标记有transient关键字。另外,如果您在此处发布这些课程,这也可能会有所帮助。HstoreUserType班级列表也可能会有所帮助。



 类似资料:
  • 问题内容: Eclipse JDT编译器似乎存在一个问题,在某些情况下,Java 8 lamda不能正确反序列化,而是抛出。我正在使用最新发布的维护版本,如下所示: 现有的Bug / SO条目报告了已(至少部分地)已解决的类似问题。我已经亲自验证以下问题已解决。 访问实例字段和方法的Java 8Lambda不能反序列化 https://bugs.eclipse.org/bugs/show_bug.

  • Eclipse JDT编译器似乎有一个问题,在某些情况下,Java8个lamda没有正确反序列化,而是抛出一个。我正在使用最近的分布式维护版本,如下所示: 现有的bug/SO条目报告了在中已经(至少部分)解决的类似问题。我亲自验证了以下问题已经得到解决。 无法反序列化访问实例字段和方法的Java 8 lambda https://bugs.eclipse.org/bugs/show_bug.cgi

  • 问题内容: 我试图序列化和反序列化内部对象的数组列表: HairList对象也是一个可序列化的对象。 此代码执行返回以下错误: 排队 我不知道我在做什么错。你能给个小费吗? 更新: 解决: 仅使用HairBirt的本机数组而不是ArrayList即可工作: 代替 感谢大家的帮助。 问题答案: 不要使用-而是使用二进制数据并对它进行base64编码,以将其转换为字符串而不会丢失信息。 我强烈怀疑这是

  • 问题内容: 我是一名练习File IO技能的学生,遇到使用ObjectInputStream从文件读取对象的问题。代码始终抛出InvalidClassException,但我找不到代码是如何将其在线或通过反复试验将其抛出的。这是我的代码: 我编写了所有单独的catch块,以弄清楚抛出了什么异常,并且该异常总是抛出InvalidClassException。 这也是我的树类: 这是我的写文件功能:

  • 问题内容: 作为一个小项目,我一直在尝试做一个小事,它可以读取序列化的lambda(从本地或从FTP)并调用它们的运行函数作为测试的一部分,以测试Windows中的文件关联(即打开某些文件类型)使用特定程序打开它们),但不管如何,无论如何,它似乎从未正确地反序列化。 lambda被这样声明 并使用由ObjectOutputStream包装的[n可选] BufferedOutputStream包装的

  • 问题内容: 在hibernate状态下执行条件查询时,出现以下异常: 可能是什么问题呢? PS:虽然可能不相关,但我的hibernate版本是hibernate-4.0.1 final。 问题答案: 问题在于被引用的实体对实体有另一个引用,并且该关系未由任何-like注释进行注释。