如何在提交事务之前删除子元素?

时间:2018-07-09 14:13:53

标签: java spring hibernate

我想先删除所有书架和书,然后再删除图书馆

public void deleteLibrary(Long library_id) {
    File fileImage = new File("");
    Library library = libraryDao.get(library_id);

    User user = userDao.get(SecurityUtil.getPricipal());
    library.deleteAllLibrarian(roleDao.getRoleByName(RoleName.LIBRARIAN));
    library.deleteAllEmployee(roleDao.getRoleByName(RoleName.EMPLOYEE));

    for(Shelf s:library.getShelves()) {
         library.delete(s);
         s.setLibrary(null); //delete shelf  // a shelf contain many book
    }

    libraryDao.delete(library); //delete library
}

删除图书馆 时出现错误,因为尚未删除所有图书。

已删除的对象将通过级联重新保存(从关联中删除已删除的对象):[com.quangdat.entities.Book#7]

0 个答案:

没有答案