Spring boot jpa 删除数据和事务管理的问题实例详解
作者:hanghangde 时间:2022-07-02 12:11:43
今天我们介绍的是jpa删除和事务的一些坑,接下来看看具体内容。
业务场景(这是一个在线考试系统)和代码:根据问题的id删除答案
repository层:
int deleteByQuestionId(Integer questionId);
service 层:
public void deleteChoiceAnswerByQuestionId(Integer questionId) {
choiceAnswerRepository.deleteByQuestionId(questionId);
测试层:
@Test
public void testDeleteByQuestionId() {
choiceAnswerService.deleteChoiceAnswerByQuestionId(5);
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
}
问题1:如果各层都不加事务管理的话
@Transactional
会报这个错误
org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process ‘remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process ‘remove' call
当我们除了query外的modiy和delete外如果没有各层的方法中进行事务管理的话也就是没加@Transactional话会报错
问题2:只在test层加@Transactional
没有错误但是数据并没有被删除,在用IDEA的调试是,在执行这个测试方法的过程时还可以在choiceanswer表中进行操作并没有加锁事务并没有起作用
问题3:只在 Repository层加@Transactional
public void deleteChoiceAnswerByQuestionId(Integer questionId) {
choiceAnswerRepository.deleteByQuestionId(questionId);
System.out.println(“hehehhe”);
System.out.println("hehehhe");
// questionRepository.delete(5);
System.out.println(“hehehhe”);
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
System.out.println("hehehhe");
}
这时当执行完
choiceAnswerRepository.deleteByQuestionId(questionId);
数据里面被修改
问题4:只在 service层加@Transactional
当只有执行完service内的对应方法时数据才会被删除
问题5:在service 层和Repository都加上@transactional
当只有执行完service内的对应方法时数据才会被删除
问题6:只要在test(或者是除了service层和Repository层)加上@Transactional,不管service层和Repository层加不加@Transactional数据都不会被删除
问题7:
@Modifying
@Query(“delete from ChoiceAnswer c where c.question.id=?1 “)
@Transactional
int deleteByQuestionId(Integer questionId);
与
@Transactional
int deleteByQuestionId(Integer questionId);
有什么区别,上面的会直接执行delete语句
下面的会先执行select 再执行delete
总结:
事务管理只有在service加上事务管理才起作用,query不需要事务管理但是delete update但需要事务管理为了不在Service层不加事务管理可以再Repository层的delete uodate加上@transactional 但这样不能真正保持事务的完整性.
本文关于Spring boot jpa 删除数据和事务管理的问题实例详解的介绍就到这里,希望对大家有所帮助,欢迎大家参阅本站其他专题。
来源:https://www.2cto.com/kf/201611/569207.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
ContentProvider启动流程示例解析
用SpringBoot+Vue+uniapp小程序实现在线房屋装修管理系统
![](https://img.aspxhome.com/file/2023/3/58883_0s.png)
Android RecyclerView使用ListAdapter高效刷新数据的操作方法
![](https://img.aspxhome.com/file/2023/0/83550_0s.gif)
java中String字符串删除空格的七种方式
![](https://img.aspxhome.com/file/2023/1/63311_0s.jpg)
Mybatis plus多租户方案的实战踩坑记录
![](https://img.aspxhome.com/file/2023/6/57956_0s.jpg)
关于Mybatis-Plus Wrapper是否应该出现在Servcie类中
如何基于JavaFX开发桌面程序
mybatisPlus条件构造器常用方法小结
Seata AT模式如何实现行锁详解
![](https://img.aspxhome.com/file/2023/3/62473_0s.png)
IntelliJ IDEA 安装教程2019.09.23(最新版)
![](https://img.aspxhome.com/file/2023/5/58175_0s.jpg)
springboot后端配置多个数据源、Mysql数据库的便捷方法
Spring Boot使用profile如何配置不同环境的配置文件
![](https://img.aspxhome.com/file/2023/2/59932_0s.png)
Java StringUtils字符串分割转数组的实现
Jenkins的安装配置详解
![](https://img.aspxhome.com/file/2023/6/62166_0s.png)
复杂JSON字符串转换为Java嵌套对象的实现
解决spring.thymeleaf.cache=false不起作用的问题
![](https://img.aspxhome.com/file/2023/5/63285_0s.png)
OpenCV实现人脸识别简单程序
ELK搭建线上日志收集系统
![](https://img.aspxhome.com/file/2023/9/62419_0s.png)
Java 八种基本类型和基本类型封装类
![](https://img.aspxhome.com/file/2023/8/59898_0s.jpg)
如何使用Spring AOP的通知类型及创建通知
![](https://img.aspxhome.com/file/2023/2/61242_0s.png)