2013-02-13 37 views
0

我有型号SomeFileModel,其中包含对存储在磁盘上的文件的引用。我想将该文件与实体一起保存并一次删除它们。虽然前者并不困难,但后者的问题在于:SomeFileModel与另一个型号FilesCollectionModel多对一地关联,并且用orphanRemoval = true注释。Hibernate中是否有一些“关于删除”孤儿去除或其等价物?

虽然服务负责SomeFileModel删除该文件,当我们订购销毁实体时,其他服务(如人们所期望的)不这样做。当然,我可以让所有模型都有一些prepareDeleteion()方法,这可以通过级联调用,但我认为这不是最好的方法。

我的问题是:是否有一些注释或其他会标记方法的约定,被称为实体删除?它甚至有可能吗?或者我应该使用其他解决方案“孤儿文件收集器”会删除数据库中没有引用的文件,或者丑陋的级联方法调用?什么方法是最好的?

如果有帮助,我使用Hibernate 4和Spring Framework 3.1.0。

回答

1

是的,简短的回答是Interceptors and Events

按Hibernate的文档:

拦截

The Interceptor interface provides callbacks from the session to the app, 
allowing the application to inspect and/or manipulate properties of a 
persistent object before it is saved, updated, deleted or loaded. 

活动:

The event system can be used in addition, or as a replacement, for interceptors. 
All the methods of the Session interface correlate to an event. 
You have a LoadEvent, a FlushEvent, etc. 
+0

十分感谢!我一定会使用它。 – 2013-02-13 21:54:53