Here's the SQL plus the stack trace:
Code:
Hibernate: select parent0_.PARENT_ID as PARENT1_0_ from PARENT parent0_ where parent0_.PARENT_ID=?
Hibernate: select children0_.parent_id as parent2_1_, children0_.CHILD_ID as CHILD1_1_, children0_.CHILD_ID as CHILD1_0_, children
0_.parent_id as parent2_1_0_ from CHILD children0_ where children0_.parent_id=?
Exception in thread "main" 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:707)
at java.util.AbstractMap.hashCode(AbstractMap.java:556)
at java.util.HashMap$Entry.hashCode(HashMap.java:707)
at java.util.AbstractMap.hashCode(AbstractMap.java:556)
at java.util.HashMap.hash(HashMap.java:261)
at java.util.HashMap.put(HashMap.java:379)
at java.util.HashSet.add(HashSet.java:192)
at java.util.AbstractCollection.addAll(AbstractCollection.java:319)
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.forceInitialization(AbstractPersistentCollection.java:280)
at org.hibernate.engine.PersistenceContext.initializeNonLazyCollections(PersistenceContext.java:796)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:223)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1345)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:116)
at org.hibernate.loader.entity.EntityLoader.load(EntityLoader.java:101)
at org.hibernate.persister.entity.BasicEntityPersister.load(BasicEntityPersister.java:2471)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:351)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:332)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:113)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:167)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:79)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:621)
at test.ParentChildTest.main(ParentChildTest.java:24)
The mapping:
Code:
<class entity-name="Parent" table="PARENT">
<id name="id" column="PARENT_ID" type="integer"/>
<set name="children" inverse="true" lazy="false" >
<key column="parent_id"/>
<one-to-many entity-name="Child"/>
</set>
</class>
<class entity-name="Child" table="CHILD">
<id name="id" column="CHILD_ID" type="integer"/>
<many-to-one name="parent" entity-name="Parent" column="parent_id" lazy="false" not-null="true" />
</class>
The call (ParentChildTest.java lines 23-24):
Code:
Session session = sessionFactory.openSession().getSession(EntityMode.MAP);
Object object = session.get("Parent", new Integer(1));
Thanks,
-Kaare