I'm wondering how exactly I should implement equals() and hashcode() on my objects. Currently I am using the commons HashCodeBuilder and EqualsBuilder for every property on my User/EmailAddress objects. This causes exceptions however when the object is being loaded, due to a circular reference. As the EmailAddress objects are being added to the User, it calls hashcode on EmailAddress, which within its method calls Hashcode on User again.. which throws a Hibernate Exception. I suppose if Hibernate didn't throw the exception I would get a looping hashcode call until everything crashed... any ideas? So am I supposed to leave out all collection type and potentially lazy properties from equals/hashcode? or just not even override equals/hashcode?
Hibernate version:
3.0.5
Mapping documents:
Excerpt from User:
Code:
<set name="emailAddresses" cascade="all,delete-orphan" lazy="false" fetch="join">
<key column="USER_ID"/>
<one-to-many class="org.tecas.model.EmailAddress"/>
</set>
Excerpt from EmailAddress:
Code:
<many-to-one name="user" column="USER_ID"/>
Full stack trace of any exception that occurs:Code:
org.springframework.orm.hibernate3.HibernateSystemException: illegal access to loading collection; nested exception is org.hibernate.LazyInitializationException: illegal access to loading collection
org.hibernate.LazyInitializationException: illegal access to loading collection
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:172)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:48)
at org.hibernate.collection.PersistentSet.hashCode(PersistentSet.java:324)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:348)
at org.tecas.model.User.hashCode(User.java:108)
at org.apache.commons.lang.builder.HashCodeBuilder.append(HashCodeBuilder.java:348)
at org.tecas.model.EmailAddress.hashCode(EmailAddress.java:215)
at java.util.HashMap.hash(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:242)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollection(CollectionLoadContext.java:183)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:268)
at org.hibernate.engine.CollectionLoadContext.endLoadingCollections(CollectionLoadContext.java:249)
at org.hibernate.loader.Loader.endCollectionLoad(Loader.java:554)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:541)
at org.hibernate.loader.Loader.doQuery(Loader.java:436)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
at org.hibernate.loader.Loader.doList(Loader.java:1593)
at org.hibernate.loader.Loader.list(Loader.java:1577)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:395)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:271)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:844)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:74)
at org.springframework.orm.hibernate3.HibernateTemplate$33.doInHibernate(HibernateTemplate.java:860)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:315)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQueryAndNamedParam(HibernateTemplate.java:851)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedQueryAndNamedParam(HibernateTemplate.java:841)
at org.tecas.dao.impl.UserDaoImpl.getUser(UserDaoImpl.java:25)
at org.tecas.dao.IUserDaoTest.testGetUser(IUserDaoTest.java:26)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at com.intellij.rt.execution.junit2.JUnitStarter.main(JUnitStarter.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)