开发者

Hooking custom actions when deleting cascadingly

开发者 https://www.devze.com 2023-04-10 19:03 出处:网络
A simple case. A user has many photos. When a user gets deleted, all of his/her photos should be deleted too (rule of cascades).

A simple case. A user has many photos. When a user gets deleted, all of his/her photos should be deleted too (rule of cascades).

I want however to be able to execute some custom code right before every photo is deleted.

Unfortunately, when deleting users, all I am doing is call userDAO.deleteUser(userID), so no specific action is taken on photos (they are deleted by Hibernate itself)

Also, I don't really want the userDAO to have the knowledge that a user has photos, so this custom code should be inserted somewhere else.

I wish it were as simple as giving an OnDelete callback when I annote my entity classes, but I haven't seen any开发者_StackOverflow such specification in the Hibernate docs


Then I Think you need to apply SPring AOP on the function which deletes user.

for example:

public void deleteUser(User user){
Session session = sessionFactory.getcurrentSection();
//delete the object

}

What you need to do is to apply @Around advice

 @Pointcut("execution(* com.vanilla.dao.*.*(..))")
    public void deleteUserMethods() { }

    @Around("deleteUserMethods()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
            Object output = pjp.proceed();
            ///perform any operations on an pjp and its parameters.
            return output;
    }

I recommend you to see this example:

http://veerasundar.com/blog/2010/01/spring-aop-example-profiling-method-execution-time-tutorial/

and Spring documentation will be also very helpful:

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-schema

0

精彩评论

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