-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: LazyInitializationException using only entity-name mappings
PostPosted: Thu Nov 05, 2009 11:04 am 
Newbie

Joined: Tue Jan 06, 2009 5:08 am
Posts: 10
Hi,
I have a problem mapping a bidirectional collection using only entities:

Code:
<hibernate-mapping>
  <union-subclass entity-name="Company" table="COMPANY">
<id name="id" column="ID" length="32" type="string">
         <generator class="uuid" />
</id>
    <property name="number" column="NUMBER" type="string" length="255" />
    <property name="companyName" column="COMPANYNAME" type="string" length="255" />
   <set name="contacts" inverse="true">
      <key column="COMPANYID" />      
      <one-to-many entity-name="Contact" />
   </set>
  </union-subclass>
</hibernate-mapping>


and the contact:

Code:
<hibernate-mapping>
  <union-subclass entity-name="Contact" table="CONTACT">
<id name="id" column="ID" length="32" type="string">
         <generator class="uuid" />
</id>
      <property name="lastName"   type="string" column="LASTNAME"/>
      <property name="firstName"   type="string" column="FIRSTNAME"/>

<many-to-one name="companyId" entity-name="Company" column="COMPANYID" not-null="false" />

  </union-subclass>
</hibernate-mapping>


Now, when I try to access the object using a join (from Contact c1 left outer join c1.contacts), I always get an lazy initialization exception:
Code:
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 java.util.HashMap$Entry.hashCode(Unknown Source)
   at java.util.AbstractMap.hashCode(Unknown Source)
   at java.util.HashMap$Entry.hashCode(Unknown Source)
   at java.util.AbstractMap.hashCode(Unknown Source)
   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:900)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:888)
   at org.hibernate.loader.Loader.doQuery(Loader.java:752)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
   at org.hibernate.loader.Loader.loadCollectionSubselect(Loader.java:2087)
   at org.hibernate.loader.collection.SubselectOneToManyLoader.initialize(SubselectOneToManyLoader.java:81)
   at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:587)
   at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionE
ventListener.java:83)
   at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1743)
   at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:476)
   at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:867)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:264)
   at org.hibernate.loader.Loader.doList(Loader.java:2228)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2125)
   at org.hibernate.loader.Loader.list(Loader.java:2120)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:361)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1148)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
...

What happens seems logical. The set tries to look in the hashtable if an object is already there, but to do so, it has to get the hash, which it can't, since the collection is not yet loaded completely.
Looking through the board, I found out, that I should overwrite the hashCode() method, which would be fine, but I do not have one. There are no Java classes (that's why I use entity-name).
What can I do?


Top
 Profile  
 
 Post subject: Re: LazyInitializationException using only entity-name mappings
PostPosted: Thu Feb 25, 2010 12:37 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
I used <class ... entity-name="... when I had to map the same class multiple times e.g. Party as sellers and as buyers.
In your example wouldn't the <union-subclass> still be nested inside a <class>?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.