Transactions 使用事务进行多次保存

Transactions 使用事务进行多次保存,transactions,spring-data,rollback,Transactions,Spring Data,Rollback,我需要在一个事务中保存服务中的更多实体-如果一个保存失败,则不应保存任何内容。我尝试使用@Transactional,但它不起作用,或者我不知道如何使用它:)保存正确的记录,而不保存坏记录,我需要两者都不保存。我怎样才能做到这一点 我的代码: public interface MyRecordRepository extends JpaRepository<Record, Long>, QueryDslPredicateExecutor<Record> {

我需要在一个事务中保存服务中的更多实体-如果一个保存失败,则不应保存任何内容。我尝试使用@Transactional,但它不起作用,或者我不知道如何使用它:)保存正确的记录,而不保存坏记录,我需要两者都不保存。我怎样才能做到这一点

我的代码:

public interface MyRecordRepository extends JpaRepository<Record, Long>,
        QueryDslPredicateExecutor<Record> {

}

@Service
public class MyPersisterServiceImpl implements MyPersisterService {...
    @Autowired
    private MyRepository myRepository;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveRecords(List<Record> recordList) {
        for(Record r : recordList){
            myRepository.save(record);
        }
    }
...


@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)
... // other annotations
public class Application extends SpringBootServletInitializer {
...
公共接口MyRecordRepository扩展了JpaRepository, QueryDSL谓词执行器{ } @服务 公共类MyPersisterService实现MyPersisterService{。。。 @自动连线 私有MyRepository MyRepository; @凌驾 @事务性(rollboor=Exception.class) 公共作废保存记录(列表记录列表){ 对于(记录r:记录列表){ 保存(记录); } } ... @启用事务管理(mode=AdviceMode.ASPECTJ) …//其他注释 公共类应用程序扩展了SpringBootServletInitializer{ ... MyEnv:Spring4.0.7和Spring数据、querydsl、hibernate、ApacheTomcat8、Java8


谢谢

aspectj配置似乎有问题,因为在代理模式下,它可以正常工作……我现在将此问题标记为已解决。

我使我的事务可以进行多次保存,如下所示:

@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public void service-method( ) throws RestServiceException,Exception{
for(entity,entities) {
     repository1.save(entity)
 }
for(entity2,entities2) {
     repository2.save(entity2)
}
throw new Exception();
}

<>希望有帮助。

1。你认为你发布的代码会编译吗?2。考虑在发布之前格式化代码。