Hibernate3 父子映射報錯:illegal access to loading collection


          org.springframework.orm.hibernate3.HibernateSystemException:?illegal?access?to?loading?collection;?nested?exception?is?org.hibernate.LazyInitializationException:?illegal?access?to?loading?collection
          Caused?by:?org.hibernate.LazyInitializationException:?illegal?access?to?loading?collection
          ????at?org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:
          341 )
          ????at?org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:
          86 )
          ????at?org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:
          355 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:
          348 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:
          309 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:
          283 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:
          150 )
          ????at?com.hiany.forum.domain.base.BaseObject.hashCode(BaseObject.java:
          32 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:
          348 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:
          309 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:
          283 )
          ????at?org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:
          150 )
          ????at?com.hiany.forum.domain.base.BaseObject.hashCode(BaseObject.java:
          32 )
          ????at?java.util.HashMap.hash(HashMap.java:
          264 )
          ????at?java.util.HashMap.put(HashMap.java:
          382 )
          ????at?java.util.HashSet.add(HashSet.java:
          194 )
          ????at?java.util.AbstractCollection.addAll(AbstractCollection.java:
          318 )
          ????at?org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:
          273 )
          ????at?org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:
          183 )
          ????at?org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:
          268 )
          ????at?org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:
          249 )
          ????at?org.hibernate.loader.Loader.endCollectionLoad(Loader.java:
          866 )
          ????at?org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:
          853 )
          ????at?org.hibernate.loader.Loader.doQuery(Loader.java:
          717 )
          ????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.forceInitialization(AbstractPersistentCollection.java:
          454 )
          ????at?org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:
          755 )
          ????at?org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:
          229 )
          ????at?org.hibernate.loader.Loader.loadEntity(Loader.java:
          1785 )
          ????at?org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:
          47 )
          ????at?org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:
          41 )
          ????at?org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:
          2730 )
          ????at?org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:
          365 )
          ????at?org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:
          346 )
          ????at?org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:
          123 )
          ????at?org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:
          177 )
          ????at?org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:
          87 )
          ????at?org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:
          862 )
          ????at?org.hibernate.impl.SessionImpl.get(SessionImpl.java:
          799 )
          ????at?org.hibernate.impl.SessionImpl.get(SessionImpl.java:
          792 )
          ????at?org.springframework.orm.hibernate3.HibernateTemplate$
          1 .doInHibernate(HibernateTemplate.java: 460 )
          ????at?org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:
          362 )
          ????at?org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:
          454 )
          ????at?org.springframework.orm.hibernate3.HibernateTemplate.get(HibernateTemplate.java:
          448 )
          ????at?com.hiany.forum.dao.HibernateGenericDao.get(HibernateGenericDao.java:
          41 )
          ????at?com.hiany.forum.dao.HibernateEntityDao.get(HibernateEntityDao.java:
          40 )
          ????at?com.hiany.forum.dao.HibernateEntityDao$$FastClassByCGLIB$$967b2136.invoke(
          < generated > )
          ????at?net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:
          149 )
          ????at?org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:
          714 )
          ????at?org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
          162 )
          ????at?org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:
          107 )
          ????at?org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:
          185 )
          ????at?org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:
          652 )
          ????at?com.hiany.forum.service.ArticleManager$$EnhancerByCGLIB$$64259eef.get(
          < generated > )
          ????at?com.hiany.forum.service.ArticleManageTest.testArticleManager(ArticleManageTest.java:
          56 )
          ????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?org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:
          69 )
          ????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?junit.framework.TestSuite.runTest(TestSuite.java:
          208 )
          ????at?junit.framework.TestSuite.run(TestSuite.java:
          203 )
          ????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 )
          在做父子映射時候,使用父類去獲取子類時候,報錯如上。
          跟蹤棧信息,我們可以發現,domain object的 hashCode 方法有錯誤。
          原來的domain object 使用了BaseObject作為基類:

          import?org.apache.commons.lang.builder.EqualsBuilder;
          import?org.apache.commons.lang.builder.HashCodeBuilder;
          import?org.apache.commons.lang.builder.ToStringBuilder;
          import?org.apache.commons.lang.builder.ToStringStyle;

          import?java.io.Serializable;

          /**
          ?*?基類,domain?object?
          ?*?繼承此類可以自動獲得toString?equals?hashCode的method。
          ?
          */

          public?abstract?class?BaseObject?implements?Serializable?{
          ????
          private?static?final?long?serialVersionUID?=?3256446889040622647L;

          ????
          public?String?toString()?{
          ????????
          return?ToStringBuilder.reflectionToString(this,
          ????????????????ToStringStyle.MULTI_LINE_STYLE);
          ????}


          ????
          public?boolean?equals(Object?o)?{
          ????????
          return?EqualsBuilder.reflectionEquals(this,?o);
          ????}


          ????
          public?int?hashCode()?{
          ????????
          return?HashCodeBuilder.reflectionHashCode(this);
          ????}

          ????
          ????
          protected?void?init(){};
          }

          但是除去該基類后,發現錯誤被修復了。
          暫時還不清楚問題為什么會發生。
          posted on 2006-10-16 09:29 旱頭憨腦 閱讀(6180) 評論(1)  編輯  收藏

          評論

          # re: Hibernate3 父子映射報錯:illegal access to loading collection 2007-12-19 14:44 d  回復  更多評論   

          原因在與pojo里面的 public int hashCode() {... } 方法調用了set對象成員的hashCode()方法
          如this.getSets().hashCode();
          刪除該方法的調用即可;調用非set對象成員的.hashCode()方法沒有問題。

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 泸西县| 丹江口市| 镇江市| 台中县| 丰顺县| 普陀区| 电白县| 柳林县| 平阳县| 沁阳市| 罗定市| 洛隆县| 巧家县| 合川市| 沂水县| 闽侯县| 元朗区| 澎湖县| 琼海市| 蓝山县| 中阳县| 普格县| 丰顺县| 崇明县| 奉节县| 深州市| 镇坪县| 衡阳市| 禹州市| 马公市| 新余市| 桐梓县| 延边| 梁河县| 东阿县| 始兴县| 剑河县| 台北市| 曲周县| 尉氏县| 颍上县|