java - Spring 交易 : What will happen if I don't give @Transaction annotation on method

标签 java spring spring-mvc jpa transactions

我正在使用 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/

相关文章:

java - Spring @Transactional 方法中没有事务启动

用于测试和生产的 Spring 属性替换

java - 扩展 SimpleUrlAuthenticationFailureHandler 会产生 404 和 No mapping found 警告

java - 创建与子类相同类型的对象

java - 在 Java 上使用 Gridbag 分隔多个按钮

spring - 部署生成的 Maven Spring 项目不起作用

mysql - 错误 org.hibernate.MappingException : No Dialect mapping for JDBC type: -1

java - Spring MVC @RequestMapping 不返回任何内容,只需关闭

java - 使用 Maven 从现有插件构建 Eclipse 功能

java - 判断当前线程是否持有 Java `Lock`