1. User Entity
@Id
@JsonIgnore
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long uid;
@Column(length = 20)
private String nickname;
2. Comment Entity
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long commentId;
private String commentText;@ManyToOne
@JoinColumn(name = "writer")
private User writer;
3. CommentMapping interface
public interface CommentMapping {Long getComment_Id();
String getComment_Text();
User getWriter();
}
4. JPA쪽
Page<CommentMapping> getFrameCommentByBoard=========================================
1,2번처럼 DB가 구성되어있고 Comment 테이블에 User가 FK로 들어가 있습니다.
실제 DB에는 writer | bigint(20) 로 구성되어있고 댓글을 입력한 사람의 uid가 들어가 있습니다
저 상태로 api 호출하면 아래처럼 에러가 납니다 ㅠㅠ
Could not write JSON: Projection type must be an interface!; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Projection type must be an interface! (through reference chain: com.test.global.service.result.success.SuccessCommonResult3["data"]->org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.sun.proxy.$Proxy168["writer"])
그래서 CommentMapping에서 User를 int나 String으로 하면 에러는 없지만 DB에 들어가 있는 Uid값(숫자)이 출력됩니다ㅠㅠ
실제 필요한 데이터는 닉네임인데 writer의 데이터형을 User객체로 하면 에러가 나고 String이나 int는 uid만 나오고 ㅠㅠ 어떻게 해야 객체로 출력할 수 있나여? ㅠㅠ
https://gorokke.tistory.com/202
위 글 참고해서 코딩한건데 ㅠㅠ뭐가 문제인가요?
1. User 엔티티와 comment 매핑을 확인한다 스샷에는 연과관계 주인 반대쪽 맵드바이가 안보임 2. 인터페이스 반환타입을 바꿔준다 List 이걸로해보샘 3. 연관관계 주인은 외래키 잇는족
그리고 프로젝션은 특정필드만가져옴 아마 다가쟈와서 끼어넣는던 @Value 이거 해서 가져와야함 오픈 프로젝션 검색 ㄱ
ㅠㅠ 형 기다려봐 ㅠㅠ 해보고 올게 ㅠㅠ
아니근데
https://gorokke.tistory.com/202
요사람은 어캐 한번에 다 가져온거임?
JPA 단방향으로 매핑해도 데이터는 가져올수 는 있음 하지만 반대로 user 객체로 접근할 수 없음 DB는 되는데 말이야 그래서 양방향으로 매핑시켜주는거 그리고 저기서 문제점은 EARGR 로 설정해놔서 쿼리졸라 나감 성능 떨어짐 왠만하면 XX TOone 관계는 LAZY로하는게 좋음
그리고 프로젝션은 그닥비추 @Query 써가지고 걍 쌩 JPQL 쓰는게나음 아니면 queryDSL 그리고 jsonignore도 딱히 좋진않음