-->
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.  [ 5 posts ] 
Author Message
 Post subject: Overiding equals() proxy gotcha
PostPosted: Fri Jun 10, 2005 12:34 am 
Newbie

Joined: Fri Jun 10, 2005 12:22 am
Posts: 6
Location: Melbourne/Sydney
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();
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 1:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
we cannot intercept field access without build time enhancement. use the getters.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 2:31 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
It is possible to use custom proxies, but it must not be a problem just to call getter.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 2:42 am 
Newbie

Joined: Fri Jun 10, 2005 12:22 am
Posts: 6
Location: Melbourne/Sydney
max wrote:
we cannot intercept field access without build time enhancement. use the getters.


Thanks for your reply. It would be helpful if your documentation
5.3. Implementing equals() and hashCode() pointed out that it is recommended to use the getters.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 10, 2005 5:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
patches to the docs are welcome ,)

_________________
Max
Don't forget to rate


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.