分析hibernate中table per class hierarchy映射方式的一个异常

hibernate中,对于table per class hierarchy(每个类一张表)的映射继承方式,可能会遇到的问题
若是这样3个标T_A、T_B、T_C,T_A是主表,T_B与T_C是2个子表;
这3个表分别对应3个类A、B、C

若T_A表存在一条主键是001的记录,且同时在T_B、T_C两张表中都存在一条主键是001的记录(若通过标准的UI操作不会出现这样的记录,一般都是在开发过程中,手动修改数据,而且数据库也认为这样数据是合理的),这是,我们若想主键为001的B对象的话,hibernate就会跑出下面异常

记住:以后遇到遮掩的异常,就你的两个子表中存在相同主键的记录,只要删除一条就可以了
org.hibernate.WrongClassException: Object with id: 001 was not of the specified subclass: B (loaded object was of wrong class)
 at org.hibernate.loader.Loader.instanceAlreadyLoaded(Loader.java:1235)
 at org.hibernate.loader.Loader.getRow(Loader.java:1186)
 at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:569)
 at org.hibernate.loader.Loader.doQuery(Loader.java:689)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
 at org.hibernate.loader.Loader.loadCollection(Loader.java:1919)
 at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:36)
 at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:520)
 at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
 at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1676)
 at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:344)
 at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
 at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:138)
  .............
 at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)
 at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:161)
  .............
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:585)
 at junit.framework.TestCase.runTest(TestCase.java:154)
 at junit.framework.TestCase.runBare(TestCase.java:127)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值