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

前端 未结 4 565
眼角桃花
眼角桃花 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 04:49

    You might find a lot of controversy about this one. My stance is that you absolutely don't use a database primary key for anything in your application. It should be completely invisible. Identify your objects in your application by some other property or combination of properties.

    On the "testing persistence operations" front, what you really want is probably to check that the fields were saved and loaded correctly and maybe that the primary key got assigned some value when you saved it. This probably isn't a job for the equals method at all.

提交回复
热议问题