Spring Data Jpa 简单使用事务
对于两张表,需要顺序操作,必须全部表均操作成功才可,否则两张表不操作。
INSERT INTO device (dev_id, tenant_id, user_id)
VALUES
('bangjile', 909, 909);
INSERT INTO collectionpoint (
device_id,
tenant_id,
point_name
)
VALUES
(808,808,'ceshihahah');
@Repository
public interface DeviceEntityRepository extends JpaRepository<DeviceEntity,Integer> {}
@Repository
public interface CollectionPointRepository extends JpaRepository<CollectionpointEntity,Integer> {}
@Repository
public class TestRepository {
@Autowired
DeviceEntityRepository deviceEntityRepository;
@Autowired
CollectionPointRepository repository;
public void testMethod(){
DeviceEntity deviceEntity=new DeviceEntity();
deviceEntity.setDevId("gogogo12");
deviceEntity.setTenantId(88);
deviceEntity.setUserId(88);
deviceEntityRepository.save(deviceEntity);
CollectionpointEntity collectionpointEntity=new CollectionpointEntity();
collectionpointEntity.setDeviceId(8);
collectionpointEntity.setTenantId(88);
collectionpointEntity.setPointName("chongtu11");
repository.save(collectionpointEntity);
}
}
public void testMethod(){
DeviceEntity deviceEntity=new DeviceEntity();
deviceEntity.setDevId("gogogo122");
...
CollectionpointEntity collectionpointEntity=new CollectionpointEntity();
collectionpointEntity.setDeviceId(8);
collectionpointEntity.setTenantId(88);
collectionpointEntity.setPointName("chongtu11");
repository.save(collectionpointEntity);
}
@Transactional(readOnly = false, rollbackFor = Throwable.class)
public void testMethod(){
...
}
06-02 18:03:51.386 DEBUG 11624 --- [nio-8089-exec-1] org.hibernate.SQL : insert into public.device (dev_desc, dev_id, dev_manufacture, dev_note, dev_position1, dev_position2, dev_sn, dev_type, emp_id, tenant_id, user_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into public.device (dev_desc, dev_id, dev_manufacture, dev_note, dev_position1, dev_position2, dev_sn, dev_type, emp_id, tenant_id, user_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2017-06-02 18:03:51.392 DEBUG 11624 --- [nio-8089-exec-1] org.hibernate.SQL : insert into public.collectionpoint (device_id, point_alarm_contacts, point_alarm_flag, point_desc, point_downlimit, point_downlimit2, point_name, point_scale, point_type, point_unit, point_uplimit, point_uplimit2, tenant_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into public.collectionpoint (device_id, point_alarm_contacts, point_alarm_flag, point_desc, point_downlimit, point_downlimit2, point_name, point_scale, point_type, point_unit, point_uplimit, point_uplimit2, tenant_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
2017-06-02 18:03:51.404 ERROR 11624 --- [nio-8089-exec-1] druid.sql.Statement : {conn-10005, pstmt-20004} execute error. insert into public.collectionpoint (device_id, point_alarm_contacts, point_alarm_flag, point_desc, point_downlimit, point_downlimit2, point_name, point_scale, point_type, point_unit, point_uplimit, point_uplimit2, tenant_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "collectionpoint_point_name_device_id_tenant_id_key"
详细:Key (point_name, device_id, tenant_id)=(chongtu11, 8, 88) already exists.
................................此处为异常信息...................
2017-06-02 18:03:51.409 DEBUG 11624 --- [nio-8089-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : could not execute statement [n/a]
................................此处为异常信息...................
2017-06-02 18:03:51.410 WARN 11624 --- [nio-8089-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23505
2017-06-02 18:03:51.410 ERROR 11624 --- [nio-8089-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: duplicate key value violates unique constraint "collectionpoint_point_name_device_id_tenant_id_key"
详细:Key (point_name, device_id, tenant_id)=(chongtu11, 8, 88) already exists.
2017-06-02 18:03:51.412 INFO 11624 --- [nio-8089-exec-1] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
2017-06-02 18:03:51.413 DEBUG 11624 --- [nio-8089-exec-1] o.h.jpa.spi.AbstractEntityManagerImpl : Mark transaction for rollback
2017-06-02 18:03:51.415 DEBUG 11624 --- [nio-8089-exec-1] cResourceLocalTransactionCoordinatorImpl : JDBC transaction marked for rollback-only (exception provided for stack trace)
................................此处为异常信息...................
2017-06-02 18:03:51.421 DEBUG 11624 --- [nio-8089-exec-1] o.h.jpa.spi.AbstractEntityManagerImpl : Mark transaction for rollback
2017-06-02 18:03:51.421 DEBUG 11624 --- [nio-8089-exec-1] o.h.e.t.internal.TransactionImpl : rollback() called on an inactive transaction
2017-06-02 18:03:51.422 DEBUG 11624 --- [nio-8089-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public void com.yginsight.adminservice.app.api.TestController.test()]: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [collectionpoint_point_name_device_id_tenant_id_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
2017-06-02 18:03:51.424 DEBUG 11624 --- [nio-8089-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [public void com.yginsight.adminservice.app.api.TestController.test()]: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [collectionpoint_point_name_device_id_tenant_id_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
2017-06-02 18:03:51.424 DEBUG 11624 --- [nio-8089-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [public void com.yginsight.adminservice.app.api.TestController.test()]: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [collectionpoint_point_name_device_id_tenant_id_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
2017-06-02 18:03:51.424 DEBUG 11624 --- [nio-8089-exec-1] o.h.e.jdbc.internal.JdbcCoordinatorImpl : HHH000420: Closing un-released batch
2017-06-02 18:03:51.428 DEBUG 11624 --- [nio-8089-exec-1] o.s.web.servlet.DispatcherServlet : Could not complete request
Spring Data Jpa 简单使用事务的更多相关文章
- spring spring data jpa save操作事务
整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...
- Spring Data JPA 简单查询--接口方法
一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...
- JAVA入门[20]-Spring Data JPA简单示例
Spring 对 JPA 的支持已经非常强大,开发者只需关心核心业务逻辑的实现代码,无需过多关注 EntityManager 的创建.事务处理等 JPA 相关的处理.Spring Data JPA更是 ...
- Spring Data JPA简单使用
用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...
- spring data jpa 简单使用
通过解析方法名创建查询 通过前面的例子,读者基本上对解析方法名创建查询的方式有了一个大致的了解,这也是 Spring Data JPA 吸引开发者的一个很重要的因素.该功能其实并非 Spring Da ...
- Spring Data JPA简单查询接口方法速查
下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.(1)先按照功能进行分类 ...
- Spring Data Jpa简单了解
原文来源:http://www.cnblogs.com/xuyuanjia/p/5707681.html 以下是自己简单整理原有文章,其实就是在原来文章基础上化重点以及可能会有所删减的方式进行整理,需 ...
- Spring Data JPA 简单查询--方法定义规则
一.常用规则速查 1 And 并且2 Or 或3 Is,Equals 等于4 Between 两者之间5 LessThan 小于6 LessThanEqual 小于等于7 Gre ...
- Spring Data JPA 简单查询
一.常用规则速查 1 And 并且2 Or 或3 Is,Equals 等于4 Between 两者之间5 LessThan 小于6 LessThanEqual 小于等于7 Gre ...
随机推荐
- vue-cli3 搭建的前端项目基础模板
基于 vue-cli3 搭建的前端模板,fork 或 clone 本仓库,即可搭建完成一个新项目的基础模板,源码地址,欢迎 star 或 fork 特性 CSS 预编译语言:less Ajax: ax ...
- 计算机网络3.2&3.3(第二节介质&第三节多路复用)
有限的传播介质 双绞线 双绞线电缆 双绞线总结 2 同轴电缆 粗细电缆的传输距离 现在基本都用双绞线和光线 同轴电缆用于居民小区和家庭使用 3 光纤 光纤中以光信号的形式进行传播 正如我们现在看到这样 ...
- struts1之工作原理
版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/zkn_CS_DN_2013/article/details/34452341 1.初始化:strut ...
- Spring-data-jpa时间按照between and 查询
需求:根据一个String类型的year,要求查询出该年的所有记录: 比如根据2018年查询出2018年01月01日到2018年12月31日之间的记录: public List<Rain> ...
- C++中String字符串查找
在写C++程序中,总会遇到要从一个字符串中查找一小段子字符串的情况,对于在C中,我们经常用到strstr()或者strchr()这两种方法.而对于C++的string,我们往往会用到find(). C ...
- postman认证使用篇(五)
postman 认证使用篇(五) Authorization 尽管请求编辑器已经足够强大去构造各种各样的请求,但是有的时候你的请求可能是需要认证,那么就可以尝试使用下面的认证功能了(由于认证的参数信息 ...
- 使用 git 来管理 PCB 版本
使用 git 来管理 PCB 版本 在传统的 PCB 版本管理是复制一份,再重命名,写上日期,写上修改日志. 自从接触了 git 后,发现 git 的版本管理完全可以胜任,且可以做的更好. 原来使用商 ...
- 容器化ICT融合初体验
[编者的话]本次将分享的容器化ICT融合平台是一种面向未来ICT系统的新型云计算PaaS平台,它基于容器这一轻量级的虚拟化技术以及自动化的"微服务"管理架构,能够有效支撑应用快速上 ...
- React Native自定义导航栏
之前我们学习了可触摸组件和页面导航的使用的使用: 从零学React Native之09可触摸组件 - 从零学React Native之03页面导航 - 经过之前的学习, 我们可以完成一个自定义导航栏了 ...
- 《spring boot》8.2章学习时无法正常启动,报“ORA-00942: 表或视图不存在 ”
在学习<spring boot>一书的过程中,由于原书作者难免有一些遗漏的的地方,或者系统.软件版本不一致.框架更新等各种因素,完全安装书中源码页不能实现项目的正常启动 在8.2章节,演示 ...