当我尝试在Spring中更新对象时,Hibernate执行删除查询

时间:2012-03-12 15:35:47

标签: 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",
            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;
}

这是BookController中用于更新图书信息的功能:

@RequestMapping(method=RequestMethod.POST)
public String dataBook(@Valid BookBean bb, BindingResult result){

    if (result.hasErrors()){
        return "redirect:/books/formBook";
    }

    booksService.saveBook(bb);

    return "redirect:/books";
}

在booksService中我有这个:

@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public void saveBook(BookBean lb) {
    DataParser dp = new DataParser();

    booksDAO.sBook(dp.fromBookBeanToBook(lb));

}

在书籍中:DAO:

@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会这样做,我该如何解决?

1 个答案:

答案 0 :(得分:0)

好的,得到它,当尝试更新作者集是空的时候,所以Hibernate做了删除而不是更新。