Hibernate merge和update的区别
今天做了个测试,写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样。实体bean很简单,就id和name两个字段,接下来分别给出以下几种测试情形的控制台日志内容:
1. 数据库记录已存在,更改person的name为一个新的name。
merge方法打印出的日志如下:
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_ from
person person0_ where person0_.id=?
Hibernate: update person set name=?
where id=?
update方法打印出的日志如下:
Hibernate: update person set name=?
where id=?
2. 数据库记录已存在,更改person的name和数据库里对应id记录的name一样的值。
merge方法打印出的日志如下:
Hibernate: select person0_.id as id0_0_,
person0_.name as name0_0_ from person person0_ where person0_.id=?
此处相对于第一种情形少了update的动作 update方法打印出的日志如下: Hibernate: update person set name=? where id=?
3.
数据库记录不存在时,也就是你传的实体bean的ID在数据库没有对应的记录。
merge方法打印出的日志如下:
Hibernate: select person0_.id as id0_0_, person0_.name as name0_0_ from person person0_ where person0_.id=?
Hibernate: insert into person (name) values (?)
如果没有对应的记录,merge会把该记录当作新的记录来插入。此处我很疑惑,因为我传得person实体对象里写明了id值的,它为什么还会做插入的动作呢?
update方法打印出的日志如下:
Hibernate: update person set name=? where id=? 2009-11-22 20:59:55,359 ERROR [org.hibernate.jdbc.AbstractBatcher] -
Exception executing batch:
org.hibernate.StaleStateException: Batch update
returned unexpected row count from update [0]; actual row count: 0; expected: 1
以下的内容摘抄自网上:
当我们使用update的时候,执行完成后,我们提供的对象A的状态变成持久化状态。
但当我们使用merge的时候,执行完成,我们提供的对象A还是脱管状态,hibernate或者new了一个B,或者检索到
一个持久对象B,并把我们提供的对象A的所有的值拷贝到这个B,执行完成后B是持久状态,而我们提供的A还是托管状态
Hibernate merge和update的区别的更多相关文章
- Hibernate update 和 merge 、saveOrUpdate的区别
this.getSession().update(obj); this.getSession().merge(obj); this.getSession().saveOrUpdate(obj); 1. ...
- Hibernate的merge与update方法的区别
今天做了个测试,写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样.实体bean很简单,就id和name两个字段,接下来分别给出以下几种测试情形的控制台日志内容: 1. 数 ...
- hibernate中load,get;find,iterator;merge,saveOrUpdate,lock的区别
hibernate中load,get;find,iterator;merge,saveOrUpdate,lock的区别 转自http://www.blogjava.net/bnlovebn/archi ...
- Hibernate: save, persist, update, merge, saveOrUpdate[z]
[z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...
- 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)
1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...
- [git]merge和rebase的区别
前言 我从用git就一直用rebase,但是新的公司需要用merge命令,我不是很明白,所以查了一些资料,总结了下面的内容,如果有什么不妥的地方,还望指正,我一定虚心学习. merge和rebase ...
- pod install 和 pod update的区别
pod install 和 pod update的区别 pod install(下载并安装pod) 1,当pod file文件中有“增加pod,删除pod,修改pod”的操作之后使用. 2,pod i ...
- org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...
- 20.org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua ...
随机推荐
- C语言扩展动态内存报错:realloc(): invalid next size: 0x0000000002365010 ***
晚上被这个内存扩展崩溃的问题折腾的有点崩溃,当答案揭晓的那一刻,恍然大悟,原来如此简单. 练习题目:输入一个字符串,根据字母进行排序,说白了就是一个简单的冒泡 #include <stdio.h ...
- TX Textcontrol 使用总结一模板
以下内容纯属个人使用感想,如有问题,还望讲解!!! 简介与使用感想: TX Text Control是一套功能丰富的文字处理控件,它以可重复使用控件的形式为开发人员提供了Word中常用的文字处理功能, ...
- 从Qt谈到C++(一):关键字explicit与隐式类型转换
转载:果冻虾仁 提出疑问 当我们新建了一个Qt的widgets应用工程时.会自动生成一个框架,包含了几个文件. 其中有个mainwindow.h的头文件.就是你要操纵的UI主界面了.我们看看其中的一段 ...
- freemarker 宏嵌套nested 的使用
转载来源:http://blog.sina.com.cn/s/blog_7e5699790100z59g.html 模板页: <#assign basePath = request.contex ...
- jmeter 参数化方法
1.csv 看异步图书 JMeter实战60页
- Amazon behavior question
Amazon onsite behavior question[一亩三分地论坛面经版] - Powered by Discuz! http://www.1point3acres.com/bbs/thr ...
- 6-3-1appium iOS
环境准备 brew install carthage npm i -g ios-deploy brew install libimobiledevice --HEAD brew install ide ...
- Windows下python 安装Mysqldb模块
CMD执行 pip install mysql-python 报错如下: 1.如果报类似 Microsoft Visual C++ 9.0 is required < Unable to fin ...
- 【Gearman学习笔记】分布式处理入门
1.首先,确保你已经安装了gearmand环境并且语言绑定(language binding)已经生效. 2.涉及到的各个部分: ServerThe server, gearmand, will co ...
- ORA-03113:通信通道的文件结尾
问题: 用命令startup启动实例时,报错“ORA-03113:通信通道的文件结尾”. 解决: SQL> startup mount ORACLE 例程已经启动. Total System G ...