75480

Best practice regarding the inclusion of id of domain model on equals implementation

Question:

What is the best practice regarding implementation of equals of domain model in grails?

Do we include the id field or just the business rule relevant fields?

Answer1:

Hibernate suggests that you include just the business key / candidate key in the equals implementation. Including the id field in the equals implementation can have negative consequences if you have generated id field. Hibernate assigns id only when saving the object (if you are using generated ids). Now for example, if your new unsaved domain object is in a HashSet and you save the domain, it will generate and assign the id to the domain, the hashcode of the domain will change if your equals / hashcode is based on id field, and your domain will be lost in set.

It is suggested that you implement equals using unique immutable fields.

See references

<ol><li><a href="https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/persistent-classes-equalshashcode.html" rel="nofollow">https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/persistent-classes-equalshashcode.html</a></li> <li><a href="http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html" rel="nofollow">http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html</a></li> <li><a href="https://stackoverflow.com/questions/1638723/equals-and-hashcode-in-hibernate" rel="nofollow">Similar question</a></li> </ol>

Recommend

  • Composing two functions in lua
  • Perl : Name “main::IN” used only once, but it is actually used
  • Max Fitness stuck at local maxima in genetic algorithm implementation
  • MIPS Is it elegant to use $s0… etc reginsters inside the procedure and restore it on end
  • I have a stored procedure that needs to pull info from the same table twice. How do I use Correlatio
  • VBA: How can I keep only the date values from a string?
  • Using a support vector classifier with polynomial kernel in scikit-learn
  • Is there a shorthand for assigning from $1…$n after matching in ruby
  • Exclude devices that do not support multitasking
  • Use of single quote and double 'single quote' in PL/SQL block
  • Convert EF-based app to multi-tenant by way of context overrides
  • What does var prime = num != 1; mean?
  • Using python to determine if a timestamp is under daylight savings time
  • Uploading image using SpringMVC 4.0 Multipart
  • Teradata Database 3130 Response limit exceeded?
  • Subclassing a Pandas DataFrame, updates?
  • Solving multiple interface implementation
  • ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn&
  • Aggregate all dataframe row pair combinations using pandas
  • How to retrieve a specific string from a specific list from a file with JSON in Python
  • Crafting a LINQ based solution to determine if a set of predicates are satisfied for a pair of colle
  • Multiple flexboxes with margin-right, except the last one in the row? Without JS?
  • C#: Import/Export Settings into/from a File
  • Suppressing passwd when calling sqlplus from shell script
  • Create Instant using a negative year
  • How can I set a binding to a Combox in a UserControl?
  • Unable to decode certificate at client new X509Certificate2()
  • AJAX Html Editor Extender upload image appearing blank
  • jQuery .attr() and value
  • Why value captured by reference in lambda is broken? [duplicate]
  • Azure Cloud Service Web Role web pages do not load
  • How to set/get protobuf's extension field in Go?
  • Cassandra Data Model
  • Trying to switch camera back to front but getting exception
  • In LanguageTool, how do you create a dictionary and use it for spell checking?
  • Importing jscolor library in angular 2
  • Revoking OAuth Access Token Results in 404 Not Found
  • Why joiner is not used after Sequence generator or Update statergy
  • Recursive/Hierarchical Query Using Postgres
  • UserPrincipal.Current returns apppool on IIS