해당 경고로그는 jpa 사용을 더 쉽게 만들어주는 인텔리제이 플러그인 jpa buddy 가 띄워주는 경고로그입니다.
jpa buddy는 아래의 이유로 위 같은 경고로그를 띄웁니다.
엔티티는 일반적으로 변동 가능성이 있는 클래스입니다. 심지어 id 조차도 변경될 여지가 있습니다.
애플리케이션에서 setter를 생성하지 않는다고 하더라도 DB에 id 생성을 위임한다면 id 조차 변경될 가능성이 있습니다. (영속화 하기 전에는 null → 영속화 한 후에는 특정 숫자값을 가짐)
변경 가능성이 있는 필드로 해시코드를 생성하다 보니 문제가 생길 수 밖에 없습니다.
@Entity
@EqualsAndHashCode
public class TestEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
private Long id;
}
TestEntity는 id를 DB에서 자동 생성하는 전략을 가지고 @EqualsAndHashCode 롬복 어노테이션을 사용하고 있습니다.
<aside> 💡 @EqualsAndHashCode는 static , transient 가 아닌 모든 필드를 사용합니다.
</aside>
TestEntity testEntity = new TestEntity();
Set<TestEntity> set = new HashSet<>();
set.add(testEntity);
testEntityRepository.save(testEntity);
Assert.isTrue(set.contains(testEntity), "Entity not found in the set");
여기서 마지막 테스트 코드는 실패하게 됩니다.
현재 TestEntity에는 static, transient가 아닌 필드는 id 하나뿐이기 때문에 id 필드만을 이용해서 equals(), hashcode() 가 자동생성됩니다.