본문 바로가기

JAVA/JPA

[JPA] Entity PK 복합키 사용 - 직렬화

 

 

복합키 생성 시 직렬화(Serializable) 가 필요하다.

데이터 교환을 위해 존재하는데 직렬화 하지 않는 경우 해당 에러가 발생한다.

 

Composite-id class must implement Serializable: ""

 

 

JPA에서 직렬화를 해야하는 이유

https://www.inflearn.com/questions/17117

 

Serializable 질문 드립니다 - 인프런 | 질문 & 답변

강의를 역시나 재미나게 보고 있습니다 ㅎㅎ 가끔 블로그를 보면 entity에 Serializable을 붙이는 경우가 있는데 이것에 대한 설명 부탁드립니다. 제가 아는 한에서 Serializable은 객체를 다른 서버로(ip

www.inflearn.com

 

 

@Entity
@Builder
@Data
@NoArgsConstructor
@AllArgsConstructor
@Table(name="tb_data")
@IdClass(DataPK.class)
public class Data extends SearchData {

	@Id
	@Column(name="data_id")
    private String dataId;

	@Id
	@Column(name="user_id")
	private String userId;

 

@Data
public class DataPK implements Serializable {
	
	private String dataId;
	
	private String userId;
}

 

 

  • PK를 선언할 클래스 생성 - Serializable 의 구현체여야 함.
  • PK Column - @Id 선언.
  • Entity에서 PK Class를 @IdClass로 지정해야 함.