I have got 3 JPA entites: A, B and C.
There exists 2 bidirectional relations: One between A and B and one between B and C.
Every relation is EAGER loaded.
Pseudocode looks like this:
Class A:
Code:
@Entity
@Table(name = "A")
public class A {
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parentA")
private Set<B> bs = new HashSet<B>();
}
Class B:
Code:
@Entity
@Table(name = "B")
public class B {
@ManyToOne(optional = false)
@JoinColumn(name = "A_ID", nullable = false, updatable = false, insertable = false)
private A parentA;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "parentB")
private Set<C> cs = new HashSet<C>();
}
Class C:
Code:
@Entity
@Table(name = "C")
public class C {
@ManyToOne(optional = false)
@JoinColumn(name = "B_ID", nullable = false, updatable = false, insertable = false)
private B parentB;
}
For equals & hashcode the apache equals and hashcode builders are used.
For class B the parentA field is ignored and for class C the parentB field is ignored for equals&hashcode to omitt cycles.
Now if I load A with a query, I got the LazyInitializationException in the hashcode method while loading Set cs.
The problem could be solved, if I ignore the set cs for equals&hashcode in class B. But i need this field for the euqals&hashcode.
Is there an other solution ?
Thanks for any advice.
Exception:
Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:363)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
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:497)
at com.test.B.hashCode(Base.java:136)
at com.test.A.hashCode(Base.java:124)
at java.util.HashMap.put(Unknown Source)
at java.util.HashSet.add(Unknown Source)
at java.util.AbstractCollection.addAll(Unknown Source)
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:899)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:887)
at org.hibernate.loader.Loader.doQuery(Loader.java:751)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:258)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1886)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3063)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:907)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:875)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
at org.hibernate.type.EntityType.resolve(EntityType.java:412)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:876)
at org.hibernate.loader.Loader.doQuery(Loader.java:751)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:258)
at org.hibernate.loader.Loader.doList(Loader.java:2233)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2130)
at org.hibernate.loader.Loader.list(Loader.java:2125)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1150)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at org.hibernate.ejb.QueryImpl.getSingleResult(QueryImpl.java:116)