SpringData入门笔记(五) - 修改、删除和事务
2017-11-10 00:00
197 查看
项目获取
本部分内容,代码戳这里数据库数据
insert into `employee` (`id`, `age`, `name`) values('1','13','zhangsan'); insert into `employee` (`id`, `age`, `name`) values('2','20','test1'); insert into `employee` (`id`, `age`, `name`) values('3','21','test2'); insert into `employee` (`id`, `age`, `name`) values('4','22','test3'); insert into `employee` (`id`, `age`, `name`) values('5','20','test4'); insert into `employee` (`id`, `age`, `name`) values('6','21','test5'); insert into `employee` (`id`, `age`, `name`) values('7','22','test6'); insert into `employee` (`id`, `age`, `name`) values('8','22','test16');
更新和删除
更新和删除操作用到了一个新的注解@Modifying
一般情况下,我们需要使用
@Modifying和
@Query注解执行更新和删除
如果只使用
@Query注解,如:
@Query("update Employee o set o.age = ?2 where o.id = ?1") public void update(Integer id, Integer age);
会抛出下面异常
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1]; nested exception is java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1] ... Caused by: java.lang.IllegalStateException: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1] ... 37 more Caused by: org.hibernate.hql.internal.QueryExecutionRequestException: Not supported for DML operations [update com.dotleo.entity.Employee o set o.age = ?2 where o.id = ?1] ... 57 more
我们为它加上
@Modifying注解,如:
@Modifying
@Query("update Employee o set o.age = ?2 where o.id = ?1") public void update(Integer id, Integer age);
运行后会发现,还是报错
org.springframework.dao.InvalidDataAccessApiUsageException: Executing an update/delete query; nested exception is javax.persistence.TransactionRequiredException: Executing an update/delete query ... Caused by: javax.persistence.TransactionRequiredException: Executing an update/delete query ...
这又是为什么呢?
其实,如果是查询操作,我们没有必要事务。但是更新操作,我们要求数据的完整性,所以必须为操作添加事务。
事务的使用
有Java EE开发经验的人肯定知道,如果一次需要操作多张表(执行多个DAO),业务逻辑会写在Service里,这样一个Service方法中调用的所有DAO层方法都被集合到一个事务中,保证了数据的完整性。因此,需要先建立
EmployeeService,然后再调用
EmployeeRepository中的方法。
注意,在Service的方法中,我们必须为之添加
@Transactional注解,让它支持事务,才能运行成功。
package com.dotleo.service; import com.dotleo.repository.EmployeeRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; /** * @author LiuFei * @create 2017-11-10 22:57 */ @Service public class EmployeeService { @Autowired private EmployeeRepository employeeRepository; @Transactional public void update(Integer id, Integer age) { employeeRepository.update(id, age); } }
删除操作不过是sql语句的变化,其他更为复杂的更新操作也不过是sql语句的变化,请加练习有助于记忆。
最终源代码:戳这里
相关文章推荐
- MySQL 24小时入门笔记(4)表的创建、修改、删除
- Elasticsearch入门CRUD(新增、查询、修改、删除)
- swift:CoreData简单入门(增加、查询、修改、删除)(详细讲解)
- OpenLDAP学习笔记8——LDAP常用操作:添加、删除、修改、搜索
- xml学习笔记③PHP DOM--对xml文件进行修改和删除操作
- Lucene5 学习笔记(2) —— 简单介绍 Lucene 搜索功能和索引的修改、删除
- lucene&solr从入门到精通-----删除,修改,查询
- EntityFramework 入门 CRUD(新增、查询、修改、删除)
- SQLite 入门教程(二)创建、修改、删除表
- NuttX 入门笔记3 STM32F103C8T6(STM32f103-minimum) 修改example点亮流水灯
- GIT入门笔记(11)- 多种撤销修改场景和对策--实战练习
- MySQL入门 及 新建用户,授权,删除用户,修改密码,
- SQLite 入门教程(二)创建、修改、删除表
- MySQL学习笔记(5) - 修改和删除数据库
- SpringData入门笔记(三) - SpringData初尝鲜
- MS SQL入门基础:定位修改删除游标
- GIT入门笔记(10)- 多种撤销修改场景和对策
- GIT入门笔记(12)- 删除文件、提交删除和恢复删除
- MySQL快速入门12----删除数据 & 备份还原 & 视图 & 事务 & 触发器
- mysql学习笔记(四)数据库及表的修改和删除