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

前端 未结 4 563
眼角桃花
眼角桃花 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:52

    I would write my test to explicitly check for fields. To make this easy, before performing the assertEqual test, I will set the id of both the expected and actual result to the same predefined value and then use the normal equals method.

    Removing ID from equals is not justifiable, just because testing is slightly difficult. You are foregoing serious performance benefits and also code integrity.

提交回复
热议问题