Should the id field of a JPA entity be considered in equals and hashCode?

前端 未结 4 573
眼角桃花
眼角桃花 2021-02-13 04:30

I hit a problem when writing tests for a database application using JPA2 and EclipseLink:

I add some entity to a database, retrieve it later and want to compare it to an

4条回答
  •  孤独总比滥情好
    2021-02-13 05:07

    From the book Hibernate in Action, its recommended to defined a business key and test equality on that. A business key is "a property, or some combination of properties, that is unique for each instance with the same database identity." In other areas it says to not use the id as one of those properties, and don't use values in collections.

提交回复
热议问题