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?