반응형

JPA Entity에서 복합키(composite key)를 사용할 떄 복합키에 해당하는 컬럼 모두에 @Id

을 달아주어야 한다.

 

그 뿐만 아니라 class에도 @IdClass(클래스명.class)를 달아주어야 한다.

이렇게만 선언해주고 실행을 하게 되면 다음과 같은 에러를 만나게 된다.

Caused by: org.hibernate.MappingException: Composite-id class must implement Serializable:

해당 클래스에 Serializable을 implements해줍니다.

Serializable을 해주어야하는 이유는 stackoverflow의 설명으로 대신하겠습니다. (저도 JPA를 막 학습하고 사용하고 있는 입장은 아니라서..특정 이슈 때마다 검색으로 적용해 나가고 있긴 한 데 한 번 제대로 된 학습이 필요할 것 같긴합니다.)

The session object needs to be serializable hence all objects referenced by it must be serializable as well. The id is used as a key to index loaded objects in the session. In case of CompositeId s the class itself is used as the id.

ref : https://stackoverflow.com/questions/9271835/why-composite-id-class-must-implement-serializable

반응형

+ Recent posts