웹개발/Jpa
[Jpa] 조회시 첫째 줄만 반복해서 출력되는 문제와 해결방법
kmhan
2021. 12. 4. 16:47
728x90
반응형
1. 문제 : 조회시 Jpa든, Spring boot Jpa(querydsl)든지 상관없이 첫째 줄만 반복해서 출력되는 문제가 있음
2. 원인 : 테이블의 기본키가 2개인데 모델에 @Id를 하나만 설정했음
3. 해결과정중에 발생한 에러
Q) @Id를 2개 넣으면 되지 않을까?
A) 에러발생 : Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Composite-id class must implement Serializable
4. 가설 : idClass에 직렬화가 반드시 필요하다고 하면 생성하면 되지 않을까?
5. 해결방법
1) 복합키가 담긴 IdClass
|
1
2
3
4
|
public class ExamId implements Serializable {
private int pid;
private int examNum;
}
|
cs |
2) Entity
|
1
2
3
4
5
6
7
8
9
10
11
12
|
@Data
@Entity
@Table(name = "exam")
@IdClass(ExamId.class)
public class Exam {
@Id
private int pid;
@Id
private int examNum;
...
}
|
cs |
참고 : https://developmentnotepad.tistory.com/entry/Spring-%EC%9E%90%EB%B0%94-%EC%A7%81%EB%A0%AC%ED%99%94-Serializable?category=805012
728x90
반응형