手动递归删除抛出“已删除实例传递给合并”冬眠中

发布于 2024-10-11 20:11:31 字数 861 浏览 8 评论 0原文

注意:我使用的是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-10-18 20:11:31

现在我知道答案了,感觉自己有点傻。我正在调用“删除(文件夹);”在我调用递归函数之后,这意味着代码尝试删除该文件夹两次。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文