spring boot jpa update 操作
2017-05-10 20:55
549 查看
@Query("update IndMsg e set e.is_read = '1' where e.user_id = :user_id ") @Modifying Integer batchRead(@Param("user_id") Integer user_id);
报错:Executing an update/delete query
按此帖http://blog.csdn.net/lzwglory/article/details/18839087
在service上加上
@Transactional
ok,解决。
另有一篇文章,写的比较好:
http://blog.csdn.net/hanghangde/article/details/53241150
有好多文章介绍了spring boot 博客已经很多了比如说:
http://blog.csdn.net/linzhiqiang0316/article/details/52639265
http://blog.csdn.net/linzhiqiang0316/article/details/52638039
http://blog.didispace.com/springbootmultidatasource/等等
上面几个博客都是有关查询的一些例子,但是今天我们收的是jpa删除和事务的一些坑
使用的spring boot版本:1.3.7.RELEASE
一.业务场景(在线考试系统)和代码:
业务逻辑:根据问题的id删除答案
repository层:int deleteByQuestionId(Integer questionId);1
1
service 层:
public void deleteChoiceAnswerByQuestionId(Integer questionId) { choiceAnswerRepository.deleteByQuestionId(questionId);1
2
1
2
test(测试)层:
@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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
二.问题
问题1:对于modify和move时如果各层都不加事务管理的话会报这个错误
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' call1
1
当我们除了query外的modiy和delete如果各层的方法中没有进行事务管理的话(没加@Transactional)话会报错
问题2:只在test层加@Transactional
没有错误但是数据并没有被删除,在用IDEA的调试是,在执行这个测试方法的过程时还可以在choiceanswer表中进行操作并没有加锁事务并没有起作用
问题3:只在 Repository层加@Transactional
public void deleteChoiceAnswerByQuestionId(Integer questionId) { choiceAnswerRepository.deleteByQuestionId(questionId);1
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");
}
2
3
4
5
6
7
8
9
10
11
12
13
1
2
3
4
5
6
7
8
9
10
11
12
13
这时当执行完
choiceAnswerRepository.deleteByQuestionId(questionId);
数据里面被修改,修改成功
问题4:只在 service层加@Transactional
当只有执行完service内的对应方法时数据才会被删除(只有在这一层加事务管理才是真正的事务管理)
问题5:在service 层和repository层都加上@transactional
当只有执行完service内的对应方法时数据才会被删除
问题6:只有在test层加上@Transactional,不管service层和repository层加不加@Transactional(可以加可以不加)
数据都不会被删除
问题7:repository层不同注解的执行结果
@Modifying @Query("delete from ChoiceAnswer c where c.question.id=?1 ") @Transactional int deleteByQuestionId(Integer questionId); 与 @Transactional int deleteByQuestionId(Integer questionId);1
2
3
4
5
6
7
1
2
3
4
5
6
7
有什么区别,上面的会直接执行delete语句
下面的会先执行select 再执行delete
总结:
事务管理只有在service加上事务管理才起作用,query不需要事务管理但是delete update modify都需要事务管理。为了不在service层不加事务管理可以在repository层的delete update modify加上@transactional 但这样不能真正保持事务的完整性相关文章推荐
- Springboot 之 JPA数据库操作进阶篇
- 在spring Boot中使用Spring-data-jpa操作数据库
- 170622、springboot编程之JPA操作数据库
- SpringBoot 使用JPA操作数据库
- SpringBoot jpa update
- Springboot 之 使用JPA进行分页操作
- SpringBoot-创建RESTful风格的 http接口访问jpa 来操作数据库
- spring boot 的 JPA 操作,提示Entity过时,No identifier specified for entity
- 在spring Boot中使用Spring-data-jpa操作数据库
- Spring Boot+JPA+Mysql完成数据库整合操作
- springboot JPA操作数据库
- 三、springboot项目的简单使用之:JPA使用操作数据库
- 两张表(多对多关联)操作 with Spring Boot, Spring Data JPA, H2
- SpringBoot intercept拦截器中获取JPA数据库操作实例
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作
- • Spring Boot 进 阶- 数 据 访 问-使用spring-data-jpa 操作数据库
- spring boot-jpa整合QueryDSL来简化复杂操作
- spring使用jpa进行update操作
- SpringBoot入门:(2)jpa方式操作数据库
- SpringBoot中连接MYSQL数据库,并使用JPA进行数据库的相关操作