将@Transactional 与传播属性一起使用时出现编译错误
Compilation error when using @Transactional with propagation attribute
我使用 this 教程开发了一个 spring 数据 jpa 程序。然后通过添加新的 class/method 来修改它来测试 spring 的 @Transactional 注释。
@Transactional
public void txnMethod() {
repository.save(new Customer("First Customer",""));
repository.save(new Customer("Second Customer",""));
...
}
以上代码正确编译和执行。
然后我修改了代码以明确设置传播模式,如下所示,但这给了我一个编译错误 - "The attribute propagation is undefined for the annotation type Transactional"
@Transactional(propagation=Propagation.REQUIRED)
public void txnMethod() {
repository.save(new Customer("First Customer",""));
repository.save(new Customer("Second Customer",""));
...
}
如何明确指定传播模式?
以下是 build.gradle 中的依赖项。我正在使用 spring 引导版本 1.2。1.RELEASE
dependencies {
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web")
compile ("org.springframework.boot:spring-boot-starter-tomcat")
compile("com.h2database:h2")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
当使用对 Spring 数据具有直接或传递依赖性的应用程序时,两个 class 名称为 @Transactional
的元素在编译时可用 class小路。其中之一是 javax.persistence.Transactional
,另一个是 org.springframework.transaction.annotation.Transactional
。就是后面的class必须用Spring进行声明式事务管理。枚举Propagation
也只有后者支持。前者支持不同的枚举,称为 TxType
.
请确保您正在应用的 @Transactional
属于 org.springframework.transaction.annotation.Transactional
类型,因为 IDE 有时会在用户键入 @Transactional
时添加对 javax.persistence.Transactional
的导入。然后,尝试将 Propagation
添加到注释失败,因为 javax.persistence.Transactional
不支持此枚举。
我使用 this 教程开发了一个 spring 数据 jpa 程序。然后通过添加新的 class/method 来修改它来测试 spring 的 @Transactional 注释。
@Transactional
public void txnMethod() {
repository.save(new Customer("First Customer",""));
repository.save(new Customer("Second Customer",""));
...
}
以上代码正确编译和执行。 然后我修改了代码以明确设置传播模式,如下所示,但这给了我一个编译错误 - "The attribute propagation is undefined for the annotation type Transactional"
@Transactional(propagation=Propagation.REQUIRED)
public void txnMethod() {
repository.save(new Customer("First Customer",""));
repository.save(new Customer("Second Customer",""));
...
}
如何明确指定传播模式? 以下是 build.gradle 中的依赖项。我正在使用 spring 引导版本 1.2。1.RELEASE
dependencies {
compile("org.springframework.boot:spring-boot-starter-jdbc")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("org.springframework.boot:spring-boot-starter-web")
compile ("org.springframework.boot:spring-boot-starter-tomcat")
compile("com.h2database:h2")
providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
}
当使用对 Spring 数据具有直接或传递依赖性的应用程序时,两个 class 名称为 @Transactional
的元素在编译时可用 class小路。其中之一是 javax.persistence.Transactional
,另一个是 org.springframework.transaction.annotation.Transactional
。就是后面的class必须用Spring进行声明式事务管理。枚举Propagation
也只有后者支持。前者支持不同的枚举,称为 TxType
.
请确保您正在应用的 @Transactional
属于 org.springframework.transaction.annotation.Transactional
类型,因为 IDE 有时会在用户键入 @Transactional
时添加对 javax.persistence.Transactional
的导入。然后,尝试将 Propagation
添加到注释失败,因为 javax.persistence.Transactional
不支持此枚举。