-->
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.  [ 1 post ] 
Author Message
 Post subject: hash code and eager collections
PostPosted: Thu Apr 16, 2009 3:51 pm 
Newbie

Joined: Thu Apr 16, 2009 12:16 pm
Posts: 3
Location: Ottawa CND
So I have a simple relationship between 2 objects. I cannot share the code directly as it proprietary in nature. I could try to reproduce a dummy test that has the same issue; however before I go through that effort I wanted to see if there was anyone that could offer an explanation or solution to my problem off the top of their head. I know that there are others that have encountered a similar issue; however they didn't present any real workable solutions (http://forum.hibernate.org/viewtopic.ph ... 13c4cc7190). What Paulj describes is exactly the issue that I am facing. I have implemented a hashcode calculation that goes over a collection of objects. This collection of objects cannot be simply ignored for the purposes of the hashcode calculation as these relationships to various object defines the uniqueness for this type.

So how do I get to the exception state. Simple. The object with the collection gets added to a Set of such objects. As it is being added to a Set the hashcodes for all the objects within that set are calculated. When calculating the hashcode for these objects the collection is inspected. At this stage the collection is being initialized (as it is eagerly fetched). Because it is being initialized the famous LazyInitializationException is thrown!

Code:
Caused by: org.hibernate.LazyInitializationException: illegal access to loading collection
   at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:341)
   at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:86)
   at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:411)
   at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:881)
   at company.artemis.model.impl.ViewSubnetImpl.hashCode(ViewSubnetImpl.java:117)
   at java.util.HashMap.put(HashMap.java:372)
   at java.util.HashSet.add(HashSet.java:200)
   at java.util.AbstractCollection.addAll(AbstractCollection.java:305)
   at org.hibernate.collection.PersistentSet.endRead(PersistentSet.java:329)
   at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:240)
   at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:222)
   at org.hibernate.engine.loading.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:195)
   at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:877)
   at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:865)
   at org.hibernate.loader.Loader.doQuery(Loader.java:729)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
   at org.hibernate.loader.Loader.loadEntity(Loader.java:1860)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:48)
   at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:42)
   at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3049)
   at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:399)
   at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:375)
   at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:139)
   at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:98)
   at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:878)
   at org.hibernate.impl.SessionImpl.immediateLoad(SessionImpl.java:836)
   at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:66)
   at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
   at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)


But this only happens when the collection is eagerly loaded! I have tried to initialize the collection first however that also results in an assertion error. Seems odd that there is no way to block the current thread until this collection is properly initialized. Any ideas out there?

Hibernate version: 3.2.6

Mapping documents: annotation based in proprietary code

Code between sessionFactory.openSession() and session.close(): proprietary

Full stack trace of any exception that occurs: see above

Name and version of the database you are using: Oracle 11g

The generated SQL (show_sql=true): proprietary

Again sorry about the lack of code but if an example is truly required I could try to create one. Just wanted to see if anyone had any interest in helping me with this problem first.[/code]


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

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.