2016-07-01 7 views
2

springアプリケーションを使用して、単一のトランザクションで下のオブジェクトを挿入する方法。オブジェクトの下の誰かがトランザクションのロールバックを挿入できない場合Spring BootアプリケーションでJPAトランザクションを実装する方法は?

clientInBoundFilesService.saveClientInBoundFiles(clientInBoundFiles); 
icdCodeService.saveICDCode(icdCode); 
cptCodeService.saveCPTCode(cptCode); 
insuranceService.saveInsurance(insurance); 
referingProviderService.saveReferingProvider(referingProvider); 

はここにここに私のJpaRepositoryFactory.javaである私のpom.xml

<parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.3.5.RELEASE</version> 
    </parent> 
    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <start-class>org.sam.application.Application</start-class> 
     <java.version>1.6</java.version> 
    </properties> 
    <dependencies> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-jdbc</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-jpa</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.10</version> 
     </dependency> 

です。

@Component 
public class JpaRepositoryFactory { 

    @PersistenceContext 
    private EntityManager entityManager; 

    public <T> T getRepository(Class clazz) { 
     notNull(clazz); 
     notNull(entityManager); 
     T crudRepository = (T) new SimpleJpaRepository(clazz, entityManager); 
     return crudRepository; 
    } 
} 

は、ここに私のサービスクラス

@Service 
public class ClaimDetailService { 

    private JpaRepositoryFactory jpaRepositoryFactory; 
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate; 

    @Autowired 
    public ClaimDetailService(JpaRepositoryFactory jpaRepositoryFactory) { 
     this.jpaRepositoryFactory = jpaRepositoryFactory; 
    } 

    @Autowired 
    public void setDataSource(DataSource dataSource) { 
     this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource); 
    } 

    @Transactional 
    public void saveClaimDetail(ClaimDetail claimDetail) { 
     JpaRepository<ClaimDetail, Long> mailAuditLogLongJpaRepository = jpaRepositoryFactory.getRepository(ClaimDetail.class); 
     mailAuditLogLongJpaRepository.save(claimDetail); 
    } 

} 

答えて

4
clientInBoundFilesService.saveClientInBoundFiles(clientInBoundFiles); 
icdCodeService.saveICDCode(icdCode); 
cptCodeService.saveCPTCode(cptCode); 
insuranceService.saveInsurance(insurance); 
referingProviderService.saveReferingProvider(referingProvider); 

いくつかの方法当量を使用して、コードをラップしています。 (あなたは、新しいサービスを作成し、必要なサービスを注入することができる)@Transactional伝播がPropagation.REQUIREDさ

@Transactional 
void allInOneTransaction(){ 
    clientInBoundFilesService.saveClientInBoundFiles(clientInBoundFiles); 
    icdCodeService.saveICDCode(icdCode); 
    cptCodeService.saveCPTCode(cptCode); 
    insuranceService.saveInsurance(insurance); 
    referingProviderService.saveReferingProvider(referingProvider); 
} 

デフォルト - > は現在のトランザクションをサポートし、何も存在しない場合は、新しいものを作成します。

関連する問題