Do I need to check all field in equals method of model class in JPA?

Do I need to validate the whole field in the equals method of the model class in JPA or use just id?

+2


a source to share


3 answers


No, there is no need to put all your fields in the equals method just because you are using JPA. Actually, specific JPA fields that you can ignore.



You need to decide what makes sense for your application, but it is quite common to exclude @Id and @Version fields. Include only those fields that distinguish one instance of an object from another: essentially the fields that form the key.

+3


a source


Not always. You should clearly see which fields form your own (business) key and only compare those fields. Sometimes all fields form their own key, sometimes they don't.



Read this discussion on the hibernate forum about the equals and hashcode implementation strategy .

+2


a source


I would only check the ID. In case it is not installed (not installed), which is the case for newly created objects, I would rely on the standard equivalent implementation (reference comparison).

-1


a source







All Articles