jpa를 활용해서 값을 넣으려고 하는데 계속해서 아래와 같이 에러가 뜬다.
도대체 이유가 뭘까?
jpa를 통해 삭제하고 update하는 과정에 하이버네이트가 객체를 잃어버려서 생긴 오류라고 생각했다.
nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:
객체 파일 위치
org.springframework.orm.jpa.JpaSystemException: A collection with cascade="all-delete-orphan" was no longer referenced
by the owning entity instance: 객체 파일 위치
nested exception is org.hibernate.HibernateException: A collection with cascade="all-delete-orphan" was no longer referenced by the owning entity instance:
객체 파일 위치
다른 객체와 연결해서 db를 저장할 때, 매핑관계를 제대로 확인해야 한다.
예를 들어 서점에서 책을 저장하는 코드를 짠다고 한다면
@Entity
@Getter @Setter
public class BookStore {
@Id @GeneratedValue
private Integer id;
private String name;
@OneToMany(mappedBy = "bookStore")
private Set<Book> books = new HashSet<>();
public void add (Book book) {
book.setBookStore(this); //이 부분 필수
this.books.add(book);
}
}
출처: 백기선 JPA, OneToMany 양방향 관계 "MappedBy" 해설
https://www.youtube.com/watch?v=hsSc5epPXDs