Hibernate 3.x "org.hibernate.LazyInitializationException: illegal access to loading collection"异常解决方法
2010-09-03 08:12
591 查看
最近在使用Hibernate 3.x做一个小项目,但是在持久化一些类的时候抛出以下异常
后来发现是自己的domain类继承了一个BaseBean,而那个BaseBean使用了apache commons-lang包里的一些类,比如:ToStringBuilder, EqualsBuilder和HashCodeBuilder,代码大致如下:
而以上异常就是由于其中的
引起的,解决办法就是不要继承父类的hashCode方法,或者自己写个hashCode方法。
Sep 2, 2010 12:56:40 PM org.hibernate.LazyInitializationException <init> SEVERE: illegal access to loading collection org.hibernate.LazyInitializationException: illegal access to loading collection at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:434) at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:881) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:174) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:348) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:387) at com.platform.gui.framework.model.BaseBean.hashCode(BaseBean.java:26) at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:881) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:174) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:348) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:387) at com.platform.gui.framework.model.BaseBean.hashCode(BaseBean.java:26) at java.util.HashMap.put(HashMap.java:418) at java.util.HashSet.add(HashSet.java:194) at java.util.AbstractCollection.addAll(AbstractCollection.java:318) at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:352) at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:260) at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:245) at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:218) at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:921) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:909) at org.hibernate.loader.Loader.doQuery(Loader.java:773) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270) at org.hibernate.loader.Loader.loadCollection(Loader.java:2082) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853) at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:479) at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:899) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:275) at org.hibernate.loader.Loader.doList(Loader.java:2449) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192) at org.hibernate.loader.Loader.list(Loader.java:2187) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347) at TestDB.testRes(TestDB.java:103) at TestDB.main(TestDB.java:22) Exception in thread "main" org.hibernate.LazyInitializationException: illegal access to loading collection at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366) at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111) at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:434) at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:881) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:174) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:348) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:387) at com.platform.gui.framework.model.BaseBean.hashCode(BaseBean.java:26) at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:881) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionAppend(HashCodeBuilder.java:174) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:348) at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:387) at com.platform.gui.framework.model.BaseBean.hashCode(BaseBean.java:26) at java.util.HashMap.put(HashMap.java:418) at java.util.HashSet.add(HashSet.java:194) at java.util.AbstractCollection.addAll(AbstractCollection.java:318) at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:352) at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:260) at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:245) at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:218) at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:921) at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:909) at org.hibernate.loader.Loader.doQuery(Loader.java:773) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270) at org.hibernate.loader.Loader.loadCollection(Loader.java:2082) at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:62) at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:628) at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83) at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1853) at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:479) at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:899) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:275) at org.hibernate.loader.Loader.doList(Loader.java:2449) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2192) at org.hibernate.loader.Loader.list(Loader.java:2187) at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119) at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706) at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347) at TestDB.testRes(TestDB.java:103) at TestDB.main(TestDB.java:22)
后来发现是自己的domain类继承了一个BaseBean,而那个BaseBean使用了apache commons-lang包里的一些类,比如:ToStringBuilder, EqualsBuilder和HashCodeBuilder,代码大致如下:
import java.io.Serializable; 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; public abstract class BaseBean implements Serializable { private static final long serialVersionUID = 1L; @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); } @Override public boolean equals(Object obj) { return EqualsBuilder.reflectionEquals(this, obj); } @Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); } }
而以上异常就是由于其中的
@Override public int hashCode() { return HashCodeBuilder.reflectionHashCode(this); }
引起的,解决办法就是不要继承父类的hashCode方法,或者自己写个hashCode方法。
相关文章推荐
- Hibernate 3.x "org.hibernate.LazyInitializationException: illegal access to loading collection"异常解决方法
- 解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误
- 解决org.hibernate.QueryException illegal attempt to dereference collection 错误异常
- 解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误
- Exception in thread "main" org.hibernate.QueryException: illegal attempt to dereference collection
- 解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误
- 解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误
- 解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误
- [Trouble Shoot]hibernate中的Failed to load class "org.slf4j.impl.StaticLoggerBinder"问题的解决方法
- Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of
- org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.jav
- org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: XXXX
- 学Hibernate报:Exception in thread "main" org.hibernate.LazyInitializationException: could not initiali
- "org.hibernate.MappingException: An AnnotationConfiguration instance is... " 异常解决
- org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role
- org.hibernate.LazyInitializationException: could not initialize proxy - no Session异常解决
- EJB项目出现“org.hibernate.LazyInitializationException: could not initialize proxy - no Session”异常解决
- struts2+spring+hibernate 懒加载异常:org.hibernate.LazyInitializationException: failed to lazily initializ
- org.hibernate.LazyInitializationException的解决方法
- Exception in thread "main" org.hibernate.HibernateException: Access to DialectResolutionInfo cannot