Hi there,
I also thought it would be handy indeed to have some comparison capabilities into my pojo's.
In fact, this kind of code looks like it could be templated.
That's how I ended up installing to Commonclipse plugin. It generates four you the equals & hashcode methods, bungin' all the prop's into a HashCodeBuilder (common-lang) of all properties of that pojo.
Keeping in mind that the db generated fields after persistence should be excluded from those methods, I generated the 2 methods above.
The situation (if it makes a difference); many-to-many using an association table.
When getting an instance of one end of the relation, I can retrieve all prop's.
But iterating over the Set to get to the other side of the relation ends up into
org.hibernate.LazyInitializationException: illegal access to loading collection
This seems to be caused by the implementation of the hashcode, that also takes collection (Set) into account and calling .append with it.
(See code snippet below)
Taking the collection out of the hashcode Commonclipse-generated method resolves the problem.
But equals should also take into account the contained collections when comparing!
Is there a way around this, or is the Commonclipse just not suitable for hibernate Pojo's ?
Can someone explain roughly, point me a link, anything the reason why hashcode causes this Lazy loading exception?
regards J.
Additional info:
==========
Hibernate version: 3.05
Full stack trace of any exception that occurs:
org.hibernate.LazyInitializationException: illegal access to loading collection
Name and version of the database you are using:mysql 4
public int hashCode() {
return new HashCodeBuilder(-1773935685, -1994585227)
.appendSuper(super.hashCode())
.append(this.solutions) // this is the Set; taking it out solves the probl.
.append(this.Clash)
.append(this.statusCode)
.append(this.serviceName).append(this.serviceInternalId).append(this.serviceId).append(this.filter)
.toHashCode();
}
|