Best practices for incorporating a domain model identifier into implementation

What is the best practice for implementing equal domain models in grails?

Will we include the id field or only the corresponding business rule fields?

0
source share
1 answer

Hibernate assumes that you only include the business key / candidate key in the equals implementation. Including the id field in the equals implementation can have negative consequences if you create an id field. Hibernate only assigns an identifier when saving an object (if you use the generated identifiers). For example, if your new unsaved domain object is located in a HashSet and you save the domain, it will generate and assign an identifier to the domain, the hash code of the domain will change if your equals / hashcode is based on the id field and your domain will be lost in the set.

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

See links

+3
source

Source: https://habr.com/ru/post/985306/


All Articles