手动递归删除抛出“已删除实例传递给合并”冬眠中
注意:我使用的是 J2EE Spring MVC + Hibernate,两者都使用注释。
我有一个在休眠中建模的文件系统,其中包含文件夹和这些文件夹中的文件的层次结构。每个文件夹都引用其父文件夹,如果它们是根文件夹,则引用 null。他们没有对他们的孩子的引用,因为那里有一点多态性,我决定最好通过查询来检索孩子。无论如何,再加上我需要使用 MySQL 触发器来跟踪数据库中的历史记录,这意味着级联删除不是一个选项。
结果我必须手动删除一些东西。现在,这个递归逻辑似乎相当简单,我所要做的就是文件夹 DAO 中的以下内容:
// Pseudo-java-code
deleteFolder(Folder folder)
{
Set<Folder> folders = getFoldersInFolder(folder);
for (Folder child:folders) {
deleteFolder(child);
}
Set<File> files = fileDAO.getFilesInFolder(folder);
for (File f:files) {
fileDAO.deleteFile(f);
}
remove(folder); // method from org.execution.dao.JpaDao
}
不幸的是,当它尝试提交事务中的更改时,我不断收到“已删除的实例传递给合并”异常。 DAO 正在由一个服务调用,该服务在类的顶部放置了以下事务注释:
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
如何修复此问题?
NOTE: I am using a J2EE Spring MVC + Hibernate with both using annotations for this.
I have a file system modeled in hibernate with a hierarchy of folders and files in these folders. The folders each refers to their parent folder or null if they are a root folder. They do not have references to their children since there is a bit of polymorphism there and I decided it would be best to query to retrieve children. Regardless, that combined with the fact that I have a requirement to use MySQL triggers to track history in the database, means that Cascading delete is not an option.
As a result I have to delete things manually. Now, the recursive logic for this seems fairly straight forward, all I should have to do is the following in the folder DAO:
// Pseudo-java-code
deleteFolder(Folder folder)
{
Set<Folder> folders = getFoldersInFolder(folder);
for (Folder child:folders) {
deleteFolder(child);
}
Set<File> files = fileDAO.getFilesInFolder(folder);
for (File f:files) {
fileDAO.deleteFile(f);
}
remove(folder); // method from org.execution.dao.JpaDao
}
Unfortunately I keep getting the "deleted instance passed to merge" exception when it attempts to commit the changes in the transaction. The DAO is being called by a service which has the following transactional annotation placed at the top of the class:
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在我知道答案了,感觉自己有点傻。我正在调用“删除(文件夹);”在我调用递归函数之后,这意味着代码尝试删除该文件夹两次。
I feel kind of silly now that I know the answer. I was calling "remove(folder);" after my call to the recursive function, meaning that the code attempted to remove the folder twice.