我正在使用 Spring-Boot、Spring Rest Controller 和 Spring Data JPA。如果我不指定 @Transaction 然后也记录 get 的创建,但我想了解它是如何发生的。我的理解是 Spring 默认添加一个带有默认参数的事务但不确定它添加的位置是添加服务层还是在存储库.
public interface CustomerRepository extends CrudRepository<Customer, Long> {
List<Customer> findByLastName(String lastName);
}
@Service
public class CustomerServiceImpl implements CustomerService> {
List<Customer> findByLastName(String lastName){
//read operation
}
// What will happen if @Transaction is missing. How record get's created without the annotation
public Customer insert(Customer customer){
// insert operations
}
}
最佳答案
Spring Data JPA 在类 SimpleJpaRepository 中的 Repository 层添加了 @Transactional 注解。这是为所有 Spring Data JPA 存储库扩展的基础 Repository 类
例如
/*
* (non-Javadoc)
* @see org.springframework.data.repository.CrudRepository#save(java.lang.Object)
*/
@Transactional
public <S extends T> S save(S entity) {
if (entityInformation.isNew(entity)) {
em.persist(entity);
return entity;
} else {
return em.merge(entity);
}
}
关于java - Spring 交易 : What will happen if I don't give @Transaction annotation on method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39517632/