开发者

Is it necessary to check all the field in equals method of model class in JPA?

Is it necessary to check all th开发者_Go百科e field in equals method of model class in JPA, or can I use only id?


No, it's not necessary to put all of your fields in the equals method just because you're using JPA. In fact, JPA specific fields you may want to ignore.

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


Not always. You should clearly see which fields form a native (business) key and compare only those fields. Sometimes all fields form the native key, sometimes not.

Read this discussion at hibernate forum about strategies of implementing equals and hashcode.


I would check the id only. In case it's null (not set), which is the case for just created objects, I would rely on standard equals implementation (comparison of references).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜