발생한 예외 내용
Caused by: org.hibernate.AnnotationException:
Collection 'com.example.jisoo_blog.entity.Post.comments'
is 'mappedBy' a property named 'posts' which does not exist in the target entity
'com.example.jisoo_blog.entity.Comment'
해석해보면 Post의 comments 콜렉션이 posts와 mappedBy되어 있는데, Comment Entity에는 그런 필드명이 없다. 는 것 같다.
Post의 코드
@Table(name="posts")
public class Post extends Timestamped {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
...
@OneToMany(mappedBy = "posts", cascade = CascadeType.REMOVE)
private List<Comment> comments;
}
Comment의 코드
@Table(name = "comments")
public class Comment extends Timestamped {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "postId", nullable = false)
private Post post;
...
}
mappedBy = "ㅁㅁㅁ" 오류.
나는 ㅁㅁㅁ 여기에 table 명이 들어가야 하나 해서 "posts"로 했더니 Entity 필드명이 들어가야 한다.
Comment의 필드가 Post post로 되어 있으므로 "post"로 해줘야 한다.
다음과 같이 고쳐 해결하였다.
@OneToMany(mappedBy = "post", ...)
'STUDY > SpringBoot' 카테고리의 다른 글
[TIL] SpringBoot dependency 버전 맞추기! (0) | 2023.07.25 |
---|---|
[TIL] SQLSyntaxError ( 컬럼명 like 문제) (0) | 2023.07.14 |
[TIL] DefaultHandlerException 예외발생(추가공부 필요) (0) | 2023.07.11 |
[TIL] http.authorizeHttpRequests.requestMatchers API 설정(정확한 해결 방법 추가 기록 필요) (0) | 2023.07.10 |
[TIL] UnsatisfiedDependencyException : Unsatisfied dependency expressed through constructor parameter 0 해결하기 (0) | 2023.06.27 |