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: Implementing Object.Equals and NonUniqueObjectException
PostPosted: Thu Sep 13, 2007 3:40 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
In trying to follow best practices, all our entities implement Object.Equals for business-meaning equality. So, foolishly I thought I should start using Object.Equals rather than == (Object.ReferenceEquals) when comparing entities. All it ended up doing is masking problems where I had duplicate entities with the same key, which cause a NonUniqueObjectException when you try to save the duplicate instance that the current session didn't fetch.

What use is Object.Equals for entities? If it ever returns true when == returns false, you're already screwed because you'll get NonUniqueObjectException, unless you're working completely detached from a session (no objects need further lazy loading or saving).


Top
 Profile  
 
 Post subject: Re: Implementing Object.Equals and NonUniqueObjectException
PostPosted: Sat Sep 15, 2007 3:26 am 
Regular
Regular

Joined: Fri Jan 20, 2006 7:45 pm
Posts: 97
Location: San Antonio, TX
Nels_P_Olsen wrote:
In trying to follow best practices, all our entities implement Object.Equals for business-meaning equality. So, foolishly I thought I should start using Object.Equals rather than == (Object.ReferenceEquals) when comparing entities. All it ended up doing is masking problems where I had duplicate entities with the same key, which cause a NonUniqueObjectException when you try to save the duplicate instance that the current session didn't fetch.

What use is Object.Equals for entities? If it ever returns true when == returns false, you're already screwed because you'll get NonUniqueObjectException, unless you're working completely detached from a session (no objects need further lazy loading or saving).


GetHashCode is probably more what you are after, using ID to help calculate the code. McCafferty has more details on one approach:
http://devlicio.us/blogs/billy_mccaffer ... ively.aspx

Hope that helps.

_________________
Dum spiro, spero
-------------------------------
Rate my post if it helps...


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.