-->
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.  [ 2 posts ] 
Author Message
 Post subject: About equals and hashcode in hibermate java files
PostPosted: Tue Mar 08, 2005 5:23 am 
Newbie

Joined: Mon Feb 28, 2005 12:53 pm
Posts: 18
In "Hibernate in Action" I read about the best practice to implement hashcode and equals for the persistent objects.

It is not a good practice to use the "surrogate key" (database identifier property) because Hibernate doesn’t assign identifier values until an entity is saved.

Also it is not a good practice to include all persistent properties of the persistent class because instances from different sessions are no longer equal if one property is modified.

So the solution is to use a "business key" (natural key you’d use if you weren’t using a surrogate key). A business key is never null, it’s
unique, and it changes rarely (if ever).

My question arrises for generated Middlegen code which is something like the following :

public boolean equals(Object other) {
if ( !(other instanceof MyObject) ) return false;
MyObject castOther = (MyObject) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}

where "id" is the <id ... > in hibernate xml file.

So I suppose that we cannot rely on equals and hashCode methods generated by Middlegen and we must implement them using the business key. Is that true?

Thanks,
Mike


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 08, 2005 8:13 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
There is an extensive discussion of the equals/hashCode issue at http://www.hibernate.org/109.html


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