Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents: n/a
Code between sessionFactory.openSession() and session.close():
Full stack trace of any exception that occurs:
Name and version of the database you are using: MySQL 4.1
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
Overiding equals Gotcha
Problem occurs when referencing private memeber field values with Hibernated proxies. As the equals method has access to these private fields it is only natural to directly access the fields, refer to first code fragement. The problem is that hibernate proxies will only fetch the fields value on request from the fields getter. Therefor two logically equal object may return either true or false depending on the state of the lazy property load.
My question is this. Either I am missing something and I should be able to directly reference my private member variables or this is in my opinion, one hell of a gotcha for hibernate development.
[b]incorrect equals() - could return false or true dependant on state of property lazy load.[b]
/**
* @see java.lang.Object#equals(Object)
*/
public boolean equals(Object object)
{
if (!(object instanceof User))
{
return false;
}
User rhs = (User) object;
return new EqualsBuilder().append(this.username, rhs.username).isEquals();
}
[b]correct equals()[b]
/**
* @see java.lang.Object#equals(Object)
*/
public boolean equals(Object object)
{
if (!(object instanceof User))
{
return false;
}
User rhs = (User) object;
return new EqualsBuilder().append(this.getUsername(), rhs.getUsername()).isEquals();
}