-->
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: Legal use of HashCodeBuilder.reflectionHashCode()?
PostPosted: Wed Jun 30, 2004 5:55 pm 
Regular
Regular

Joined: Wed Jan 07, 2004 5:16 pm
Posts: 65
Location: CA, USA
Is it valid to use Commons Lang HashCodeBuilder.reflectionHashCode( this ) in the super class of objects to be persisted?

Ideally we want to avoid developers having to implement their own equals() and hashCode() and so the use of HashCodeBuilder and EqualsBuidler and using their reflection methods seems to be a good way to implement this.

The issue we are seeing though is that when hashCode() is being called on an object being retrieved, it looks like getters are being invoked on the persisted objects (by HashCodeBuilder) that are being retrieved before the Collections it contains have been intitialized, giving exceptions related to the loading of Collections.

Heres an example stack trace:
Code:
Caused by: net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection

at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:206)

at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)

at net.sf.hibernate.collection.Set.hashCode(Set.java:384)

at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:297)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:270)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:200)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:177)

at x.BaseBO.hashCode(BaseBO.java:171)

at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:297)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:270)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:200)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:177)

at x.BaseBO.hashCode(BaseBO.java:171)

at java.util.HashMap.hash(HashMap.java(Inlined Compiled Code))

at java.util.HashMap.put(HashMap.java(Compiled Code))

at java.util.HashSet.add(HashSet.java(Compiled Code))

at java.util.AbstractCollection.addAll(AbstractCollection.java(Compiled Code))

at net.sf.hibernate.collection.Set.endRead(Set.java:245)

at net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3048)

at net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3035)

at net.sf.hibernate.loader.Loader.doQuery(Loader.java:243)

at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)

at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:910)

at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:885)

at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:80)

at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284)

at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3226)

at net.sf.hibernate.collection.PersistentCollection.forceInitialization(PersistentCollection.java:340)

at net.sf.hibernate.impl.SessionImpl.initializeNonLazyCollections(SessionImpl.java:3089)

at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)

at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:831)

at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:851)

at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:57)

at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:49)

at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)

at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2081)

at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1955)

at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1884)

at x.BaseHibernateDAO.findById(BaseHibernateDAO.java:467)

at x.codestable.UpdateDAOHibernateImpl.getCode(UpdateDAOHibernateImpl.java:116)

at x.CodesTableActivityImpl.getCode(CodesTableActivityImpl.java:126)

... 47 more

Caused by: net.sf.hibernate.AssertionFailure: cannot access loading collection

at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:196)

at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)

at net.sf.hibernate.collection.Set.hashCode(Set.java:384)

at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:297)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:270)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:200)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:177)

at x.BaseBO.hashCode(BaseBO.java:171)

at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:297)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:270)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:200)

at org.apache.commons.lang.builder.HashCodeBuilder.reflectionHashCode(HashCodeBuilder.java:177)

at x.BaseBO.hashCode(BaseBO.java:171)

at java.util.HashMap.hash(HashMap.java(Inlined Compiled Code))

at java.util.HashMap.put(HashMap.java(Compiled Code))

at java.util.HashSet.add(HashSet.java(Compiled Code))

at java.util.AbstractCollection.addAll(AbstractCollection.java(Compiled Code))

at net.sf.hibernate.collection.Set.endRead(Set.java:245)

at net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3048)

at net.sf.hibernate.impl.SessionImpl.endLoadingCollections(SessionImpl.java:3035)

at net.sf.hibernate.loader.Loader.doQuery(Loader.java:243)

at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)

at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:910)

at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:885)

at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:80)

at net.sf.hibernate.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:284)

at net.sf.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:3226)

at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:200)

... 87 more



Is this a valid use of HashCodeBuilder? How can this problem be avoided?

Thanks,
Kevin Hooke


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 30, 2004 10:55 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
The lazy load isn't the only place this will probably break. I would think if you set up any bidirectional relationships you'll end up blowing your stack as you get into an endless loop.

i.e. You have a root object that references an object through a one to many and then the child references the parent in a many to one. Enter chaos :)

In a previous life a project I was working on had built a toString and Equals using reflection and it worked wonderously until moving to hibernate for the bidirectional reason. We weren't lazy loading at the time so that wasn't an issue.

We did however code some smarts into it such that if when looking at the child and it tried to reference back to the parent we would simply use its ID rather than try to get all of its properties through reflection.


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.