2016-09-15 2 views
3

私はSpring-Boot、Spring Rest Controller、Spring Data JPAを使用しています。 @Transactionを指定しなければ、getも作成されますが、私はそれがどうなっているのか理解したいと思います。私の理解は、デフォルトでSpringはデフォルトのパラメータでトランザクションを追加しますが、 。Springトランザクション:メソッドに@Transactionアノテーションを指定しないとどうなるでしょうか

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 
    } 
} 
+0

私は同じ質問があり、答えを見つけるのに長い時間がかかるので、投票しました。 –

答えて

0

スプリングデータJPAは、すべてのスプリングデータJPAリポジトリ用に拡張されたベースリポジトリクラスを特異クラスSimpleJpaRepository.Thisリポジトリ層に@Transactionalアノテーションを追加している

例えば

/* 
    * (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); 
     } 
    } 
0

スプリングはDAOレイヤーの@Transactionalアノテーションを自動的に追加しますが、正しい場所でも正しい動作でもありません。注釈はサービスレイヤーに配置する必要があります。回答済みの質問hereがあります。

関連する問題