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