本文介绍了源代码中的Hibernate事务注释-类和方法级别使用之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解将事务注释应用于类与方法(甚至在属性级别)时行为上的区别是什么.有没有人对此有一个牢固的联系,或者甚至可以为我简洁地捕捉这些差异?

I am trying to understand what the difference in behavior is when applying the transaction annotation to a class vs. method (and at the property level even?). Does anyone have a solid link for this or perhaps can even capture these differences succinctly for me?

我找到的有关这些注释的数百个链接似乎只是针对如何在配置文件中而不是在源文件中使用它们.而且在极少数情况下甚至提到在源代码中使用它们,这很容易使人产生波浪感.

The hundreds of links I've found regarding these annotations only seem to address how to use them in the configuration files rather than in the source. And in the rare case where using them in the source is even mentioned it's very hand wavy.

这是我在课堂上所指的具体例子:

Here's a concrete example of what I mean by class level:

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public class MyKickAssClass {
}

和相关的方法级别:

public class MyOtherKickAssClass {
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void entryPointMethod() {
        //do some work, call other methods of this class, other fun stuff
    }

我可以对这些情况下的行为做出一些猜测,但是要找到具体的解释和例子却令人头疼.

I can make some guesses about what the behavior is in these cases but finding concrete explanations and examples has been a headache.

我们正在使用JBoss.

We're using JBoss.

谢谢.

推荐答案

来自JavaDoc:

在bean类上指定TransactionAttribute批注意味着它适用于该类的所有适用业务方法.在方法上指定注释仅将其应用于该方法.如果在类和方法级别都应用了注释,则如果两者不同,则方法值将被覆盖.

Specifying the TransactionAttribute annotation on the bean class means that it applies to all applicable business methods of the class. Specifying the annotation on a method applies it to that method only. If the annotation is applied at both the class and the method level, the method value overrides if the two disagree.

http://download.oracle.com/javaee/6/api/javax/ejb/TransactionAttribute.html

我还建议阅读Java EE教程:

I also recommend reading the Java EE Tutorial:

http://download.oracle.com/javaee/6/tutorial/doc/bncih.html

这篇关于源代码中的Hibernate事务注释-类和方法级别使用之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:46