I am having trouble getting a bidirectional parent/child relation to work using dynamic entities (default_entity_mode=dynamic-map). When I try to access the child collection from the parent I get the exception below. After debugging a little it seems that there is a reentrancy issue. When I try to print out the contents of the collection, toString is called which triggers the read. When the read is just about done (in endRead), it tries to put the loaded collection (in a temp variable) in the right spot calling Collection.addAll. This eventually calls PersistentSet.hashCode() which calls the read method and BOOM! If I remove the many-to-one relationship from the child things work as expected.
I have tried to follow the best practices for creating parent/child relationships, but maybe I missed something.
Thanks in advance,
Doug
Hibernate version: 3.0.5
Mapping documents:
Code:
<class entity-name="TestParent" table="DV_PARENT">
<id name="id" type="int">
<generator class="sequence">
<param name="sequence">EN_ORG_TREES_SEQ</param>
</generator>
</id>
<property name="name" type="string"/>
<set name="children" table="dv_child" inverse="true">
<key column="id"/>
<one-to-many entity-name="TestChild"/>
</set>
</class>
<class entity-name="TestChild" table="DV_CHILD">
<id name="id" type="int">
<generator class="sequence">
<param name="sequence">EN_ORG_TREES_SEQ</param>
</generator>
</id>
<property name="name" type="string"/>
<many-to-one name="PARENT_ID" entity-name="TestParent" not-null="true"/>
</class>
Code between sessionFactory.openSession() and session.close():Code:
Transaction t = s.beginTransaction();
Query q = s.createQuery("from TestParent");
List l = q.list();
for (Iterator it = l.iterator(); it.hasNext();) {
Map m = (Map) it.next();
for (Iterator it2 = m.entrySet().iterator(); it2.hasNext();) {
Map.Entry me = (Map.Entry) it2.next();
System.out.println("key [" + me.getKey() + "] value [" + me.getValue() + "]");
}
}
t.commit();
Full stack trace of any exception that occurs:Code:
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:172)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:324)
at java.util.HashMap$Entry.hashCode(HashMap.java:687)
at java.util.AbstractMap.hashCode(AbstractMap.java:552)
at java.util.HashMap$Entry.hashCode(HashMap.java:687)
at java.util.AbstractMap.hashCode(AbstractMap.java:552)
at java.util.HashMap.hash(HashMap.java:257)
at java.util.HashMap.put(HashMap.java:375)
at java.util.HashSet.add(HashSet.java:188)
at java.util.AbstractCollection.addAll(AbstractCollection.java:315)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:242)
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:554)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:541)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:176)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:222)
at java.lang.String.valueOf(String.java:2177)
at java.lang.StringBuffer.append(StringBuffer.java:371)
at TestMain.main(TestMain.java:24)
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:172)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:324)
at java.util.HashMap$Entry.hashCode(HashMap.java:687)
at java.util.AbstractMap.hashCode(AbstractMap.java:552)
at java.util.HashMap$Entry.hashCode(HashMap.java:687)
at java.util.AbstractMap.hashCode(AbstractMap.java:552)
at java.util.HashMap.hash(HashMap.java:257)
at java.util.HashMap.put(HashMap.java:375)
at java.util.HashSet.add(HashSet.java:188)
at java.util.AbstractCollection.addAll(AbstractCollection.java:315)
at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:242)
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:554)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:541)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.loadCollection(Loader.java:1434)
at org.hibernate.loader.collection.OneToManyLoader.initialize(OneToManyLoader.java:111)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:488)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:60)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1430)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:176)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:222)
at java.lang.String.valueOf(String.java:2177)
at java.lang.StringBuffer.append(StringBuffer.java:371)
at TestMain.main(TestMain.java:24)
Name and version of the database you are using: oracle 9.0.2
The generated SQL (show_sql=true):
Quote:
Hibernate: select testparent0_.id as id, testparent0_.name as name2_ from DV_PARENT testparent0_
Hibernate: select children0_.id as id1_, children0_.id as id0_, children0_.name as name3_0_, children0_.PARENT_ID as PARENT3_3_0_ from DV_CHILD children0_ where children0_.id=?