从休眠4升级到5.2.17时,单表继承中的org.hibernate.WrongClassException

时间:2018-06-26 13:52:56

标签: java hibernate

我有以下课程:

@Entity
@Table(name = "TranzitOfficeLayer")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "all")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DTYPE", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorOptions(force = true)
public abstract class OfficeLayer implements Serializable, ir.customs.saloon.OfficeLayer, Cloneable {
...
}

@Entity
@Table(name = "EstelamAzDarbKhorujLayer")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "all")
@DiscriminatorValue("EstelamAzDarbKhorujLayer")
public class EstelamAzDarbKhorujLayer extends OfficeLayer implements Serializable {

    @Basic(optional = true)
    @Cache(include = "all", usage = CacheConcurrencyStrategy.READ_WRITE)
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @Fetch(FetchMode.SUBSELECT)
    @JoinTable(name = "TranzitOfficeLayer_DarbeKhorujOfficeLayers",
            joinColumns = @JoinColumn(name = "TranzitOfficeLayer_dbId", referencedColumnName = "dbId"),
            inverseJoinColumns = @JoinColumn(name = "darbeKhorujOfficeLayerses_serialNum", referencedColumnName = "serialNum"))
    private Collection<DarbeKhorujOfficeLayers> darbeKhorujOfficeLayerses;

    public Collection<DarbeKhorujOfficeLayers> getDarbeKhorujOfficeLayerses() {
        if (darbeKhorujOfficeLayerses == null) {
            return new LinkedList<>();
        }
        return darbeKhorujOfficeLayerses;
    }
}

@Entity
@Table(name = "EstelamAzDarbKhorujVaredat")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, include = "all")
@DiscriminatorValue("EstelamAzDarbKhorujVaredat")
public class EstelamAzDarbKhorujVaredat extends EstelamAzDarbKhorujLayer {
}

@Entity
@Table(name = "EstelamAzDarbkhorujSaderat")
@DiscriminatorValue(value = "EstelamAzDarbKhorujSaderat")
public class EstelamAzDarbKhorujSaderat extends EstelamAzDarbKhorujVaredat {
}

当我在打开的会话中间从getDarbeKhorujOfficeLayerses()类的EstelamAzDarbKhorujLayer类调用Caused by: org.hibernate.WrongClassException: Object [id=194759] was not of the specified subclass [EstelamAzDarbKhorujSaderat] : loaded object was of wrong class class EstelamAzDarbKhorujVaredat at org.hibernate.loader.plan.exec.process.internal.EntityReferenceInitializerImpl.hydrateEntityState(EntityReferenceInitializerImpl.java:190) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.readRow(AbstractRowReader.java:103) at org.hibernate.loader.plan.exec.internal.EntityLoadQueryDetails$EntityLoaderRowReader.readRow(EntityLoadQueryDetails.java:241) at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:122) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86) at org.hibernate.loader.entity.plan.AbstractLoadPlanBasedEntityLoader.load(AbstractLoadPlanBasedEntityLoader.java:167) at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4121) at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:508) at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:478) at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:219) at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:278) at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:121) at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:89) at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1240) at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1123) at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:682) at org.hibernate.type.EntityType.resolve(EntityType.java:464) at org.hibernate.type.ManyToOneType.resolve(ManyToOneType.java:239) at org.hibernate.engine.internal.TwoPhaseLoad.doInitializeEntity(TwoPhaseLoad.java:171) at org.hibernate.engine.internal.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:128) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.performTwoPhaseLoad(AbstractRowReader.java:238) at org.hibernate.loader.plan.exec.process.internal.AbstractRowReader.finishUp(AbstractRowReader.java:209) at org.hibernate.loader.plan.exec.process.internal.ResultSetProcessorImpl.extractResults(ResultSetProcessorImpl.java:133) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:122) at org.hibernate.loader.plan.exec.internal.AbstractLoadPlanBasedLoader.executeLoad(AbstractLoadPlanBasedLoader.java:86) at org.hibernate.loader.collection.plan.AbstractLoadPlanBasedCollectionInitializer.initialize(AbstractLoadPlanBasedCollectionInitializer.java:87) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:688) at org.hibernate.event.internal.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:75) at org.hibernate.internal.SessionImpl.initializeCollection(SessionImpl.java:2223) at org.hibernate.collection.internal.AbstractPersistentCollection$4.doWork(AbstractPersistentCollection.java:565) at org.hibernate.collection.internal.AbstractPersistentCollection.withTemporarySessionIfNeeded(AbstractPersistentCollection.java:247) at org.hibernate.collection.internal.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:561) at org.hibernate.collection.internal.AbstractPersistentCollection.read(AbstractPersistentCollection.java:132) at org.hibernate.collection.internal.PersistentBag.iterator(PersistentBag.java:277) 方法时,会引发以下异常:

DTYPE

数据库中EstelamAzDarbKhorujVaredat列的值为EstelamAzDarbKhorujSaderat,但是休眠状态似乎试图将该对象标识为darbeKhorujOfficeLayerses

此外,当我处于调试模式并且计算断点上<p> main-body loaded! </p> <!-- multi slot transclusion here --> <ng-content select="[body-content]"></ng-content> <ng-content select="[body-header]"></ng-content> <ng-content select="[body-footer]"></ng-content> 字段的实时值时,不会引发该异常。

此代码在hibernate 4中完美地工作,但是当我升级到hibernate 5.2.17时,发生了这种情况。 我已经搜索了许多天以找到解决方案,但没有一个起作用。 预先谢谢你!

1 个答案:

答案 0 :(得分:1)

此问题是由HHH-4742引起的。只要确保您不要仅通过基类加载实体,然后再尝试通过其子类加载它即可。


@DiscriminatorValue在映射中是多余的,因为它始终与类名匹配。尝试将其删除。

@DiscriminatorOptions#force is needed when

  

该表包含带有额外区分符值的行,这些区分符值不是   映射到持久类

如果不是这种情况,也将其删除。

如果仍然失败,请尝试按照this article中的说明进行复制。