如何使用Spring Boot应用程序实现JPA事务?

时间:2016-07-01 19:57:25

标签: spring jpa spring-boot transactions

如何使用spring Boot应用程序在单个事务中插入下面的对象。如果对象下面的任何人未能插入该事务回滚。

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

这是我的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>

这是我的JpaRepositoryFactory.java。

@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);
    }

}

1 个答案:

答案 0 :(得分:4)

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

使用某些方法eq包装代码。 (您可以创建新服务并注入所需的服务)

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

默认@Transactional传播是Propagation.REQUIRED - &gt; 支持当前事务,如果不存在则创建新事务。