分析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)

 



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章