Hibernate Aspectj驱动程序如何休眠事务

Hibernate Aspectj驱动程序如何休眠事务,hibernate,transactions,aspectj,Hibernate,Transactions,Aspectj,我将AspectJ写入自动驱动程序休眠事务: public aspect HiberAspectj { before() : execution(@AnnHibernateSession * *.*()) && !cflowbelow( execution(@AnnHibernateSession * *.*()) ) && !within(HiberAspectj) {

我将AspectJ写入自动驱动程序休眠事务:

public aspect HiberAspectj {

    before() :  execution(@AnnHibernateSession * *.*()) 
             && !cflowbelow( execution(@AnnHibernateSession * *.*())   )   
             && !within(HiberAspectj) {
        System.out.println("Start hibernate transaction");
    }

    after( ) returning() : execution(@AnnHibernateSession * *.*()) 
     && !cflowbelow( execution(@AnnHibernateSession * *.*())   )   
     && !within(HiberAspectj) {
        System.out.println("Commit");
    }

    after() throwing() : execution(@AnnHibernateSession * *.*()) 
     && !cflowbelow( execution(@AnnHibernateSession * *.*())   )   
     && !within(HiberAspectj) {
        System.out.println("Rollback");
    }
}
注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AnnHibernateSession {

}
DAO类:

public class DAO {

    public static void other() {
        System.out.println(" - other");
    }

    @AnnHibernateSession
    public static void updateEmp() {
        System.out.println(" - Update Emp");
    }

    @AnnHibernateSession
    public static void updateDept() {
        System.out.println(" - Update Dept");
    }
}
课程测试:

public class Test {

    // Call method OK (One Transaction create!!)
    @AnnHibernateSession
    public static void callAction1()  {
        DAO.updateDept();
        DAO.other();
        DAO.updateEmp();
    }

    // No @AnnHibernateSession
    // Call method not OK (2 Transaction Create!!)
    public static void callAction2()  {
        DAO.updateDept();
        DAO.other();
        DAO.updateEmp();
    }

    public static void main(String[] args) {
        callAction1();
        System.out.println(" ------------------ ");
        callAction2();

    }

}
我运行了类测试并得到了结果:

Start hibernate transaction
 - Update Dept
 - other
 - Update Emp
Commit
 ------------------ 
Start hibernate transaction
 - Update Dept
Commit
 - other
Start hibernate transaction
 - Update Emp
Commit
调用方法callAction1:创建一个事务

方法callAction2调用:创建两个事务(我不喜欢这样)

帮帮我。
如何将aspect HiberAspectj编辑为方法callAction2与callAction1一样工作?

您的aspect完全按照您指定的方式工作:它围绕使用
@AnnHibernateSession注释的任何方法创建事务,除非事务已经打开。由于
callAction2
没有事务注释,因此不会像
callAction1
那样创建括号事务。但是,由于
updateEmp
updateDep
都带有事务注释,因此会为它们中的每一个创建事务。你还指望什么?如果要更改行为,请相应地更改注释或修改方面。不过,外观看起来不错。我建议添加缺少的注释。