[Exception] [JPA]HibernateException: identifier of an instance of com.mappingtest.Locker was altered from 1 to 2
다음 그림에서 보듯, PLANT_OBJ
엔티티와 LEAF
엔티티의 매핑 관계는 OneToOne이다.
PLANT_OBJ
의 leafId를 수정하고자 할 때, 단순히 클라이언트에서 받은 leafId
를 setter로 매핑한다면 손쉽게 PLANT_OBJ 객체에 존재하는 LEAF 정보만 수정할 수 있지 않을까?라는 궁금증이 생겼다.
그러나 실천에 옮긴 순간다음과 같은 예외가 발생했다.
"org.springframework.orm.jpa.JpaSystemException: identifier of an instance of com.mappingtest.Locker was altered from 1 to 2; nested exception is org.hibernate.HibernateException: identifier of an instance of com.mappingtest.Locker was altered from 1 to 2"
HibernateException: identifier of an instance of com.mappingtest.Locker was altered from 1 to 2
- 이 예외는 예외는 JPA 엔티티의 식별자 값을 변경하려고 할 때 발생한다. JPA에서 관리되는 엔티티의 식별자 값은 영속성 컨텍스트 내에서 변경되면 안 된다. 엔티티 식별자를 변경하려면 새로운 엔티티를 생성하거나, 기존 엔티티를 삭제하고 새로운 엔티티를 추가해야 하는 것이다.