在我的应用程序中,我有no
@EnableTransactionManagement
注释(检查了x次)。
但是,如果我不将服务方法注释为
jakarta.persistence.TransactionRequiredException
,我仍然会得到 org.springframework.transaction.annotation.Transactional
。
@EnableTransactionManagement
默认启用吗?
我在这里缺少什么?
@Repository
public class PersistencePostgresImpl implements Persistence {
@PersistenceContext
private EntityManager em;
@Override
public void saveUser(User user) {
DbUserEntity entity = em.find(DbUserEntity.class, user.getUserUID());
if(entity == null){
throw new UserNotFoundException(user.getUserUID());
}
entity.setLoginName(user.getAccessData().getUserName());
entity.setUserData(user);
}
}
@Service
public class UserService {
@Inject
private Persistence persistence;
@Transactional
public void updateUserData(User user) {
persistence.saveUser(user);
}
}
我的配置是(空):
@Configuration
@PropertySource(value = "classpath:persistence.properties")
public class PersistenceConfiguration {
}
持久性.属性:
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
Springboot:3.2.1 休眠:6.4.1
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
如果您的主类上有
@SpringBootApplication
注释,您也会有 @EnableAutoConfiguration
。
这就是为什么
TransactionAutoConfiguration
中的 org.springframework.boot.autoconfigure.transaction
跳进来(很可能你的类路径上有 spring-jdbc)。
参见Spring Boot中的@EnableTransactionManagement以供参考。