Java Hibernate在Spring中尝试更新对象时执行删除查询
我有以下课程:Java Hibernate在Spring中尝试更新对象时执行删除查询,java,hibernate,spring-mvc,Java,Hibernate,Spring Mvc,我有以下课程: @Entity @Table(name="tbl_books") public class Book{ private int id_book; private String isbn; private Set<Author> authors; @ManyToMany(cascade=CascadeType.ALL, fetch= FetchType.EAGER) @JoinTable(name="tbl_books_tbl_authors",
@Entity
@Table(name="tbl_books")
public class Book{
private int id_book;
private String isbn;
private Set<Author> authors;
@ManyToMany(cascade=CascadeType.ALL, fetch= FetchType.EAGER)
@JoinTable(name="tbl_books_tbl_authors",
joinColumns={@JoinColumn(name="id_book")},
inverseJoinColumns= {@JoinColumn(name="id_author")})
public Set<Author> getAuthors() {
return authors;
}
public void setAuthors(Set<Author> authors) {
this.authors = authors;
}
在服务中,我有一个:
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void saveBook(BookBean lb) {
DataParser dp = new DataParser();
booksDAO.sBook(dp.fromBookBeanToBook(lb));
}
在booksDAO中:
@Override
public void sBook(Book book) {
sessionFactory.getCurrentSession().saveOrUpdate(book);
}
然后,当我尝试从该表单更新书籍时,Hibertate会执行以下操作:
Hibernate: update tbl_books set date=?, isbn=? where id_book=?
Hibernate: delete from tbl_books_tbl_authors where id_book=?
Hibernate为什么要这样做,我如何修复它?好的,我知道了,当尝试更新作者集时,它是空的,所以Hibernate执行了删除而不是更新
Hibernate: update tbl_books set date=?, isbn=? where id_book=?
Hibernate: delete from tbl_books_tbl_authors where id_book=?