开发者

Manual recursive delete throws "deleted instance passed to merge" in hibernate

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-jav开发者_如何学Goa-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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜