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 ...
随机推荐
- bzoj 2784 [JLOI2012]时间流逝——树上高斯消元
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2784 一个状态可以加很多个能量圈,但减少能量圈的情况只有一种.所以可以用树来刻画. 然后就变 ...
- ES之七:配置文件详解
安装流程 http://www.elasticsearch.org/overview/elkdownloads/下载对应系统的安装包(我下载的是tar的),下载解压以后运行es根目录下bin目录的el ...
- request-2高级用法
会话对象 会话对象让你能够跨请求保持某些参数.它也会在同一个session示例发出的所有请求之间保持cookie cookie与session的区别 1.cookie数据存放在客户的浏览器上,sess ...
- matplot 代码实例2
要画出如上图(注意原点有边距),怎么办呢? 简单而优雅,请看代码: #!/usr/bin/env python # coding=utf-8 import matplotlib.pyplot as p ...
- 使用Golang进行性能分析(Profiling)
转自:http://www.cppblog.com/sunicdavy/archive/2015/04/11/210308.html 本文介绍游戏服务器的性能分析, web服务器性能分析不在本文分析范 ...
- tensorflow函数学习:队列和线程
队列函数: FIFOQueue和RandomShuffleQueue 1.FIFOQueue FIFOQueue是先进先出队列,主要针对序列样本.如:使用循环神经网络时,需要处理语音.文字.视频等序列 ...
- Frequently Asked Questions
转自:http://www.tornadoweb.org/en/stable/faq.html Frequently Asked Questions Why isn’t this example wi ...
- 深入理解yield(二):yield与协程
转自:http://blog.beginman.cn/blog/133/ 协程概念 1.并发编程的种类:多进程,多线程,异步,协程 2.进程,线程,协程的概念区别: 进程.线程和协程的理解 进程:拥有 ...
- 【Linux_Unix系统编程】chapter7 内存分配
Chapter7 内存分配本章将用于在堆或者栈上分配内存的函数.7.1 在堆上分配内存 通常将堆的当前的内存边界称为"program break" 7.1.1 调整program ...
- 关于raid5的一系列问题
前几天我的一个同事在对计划采购的存储进行测试,期间聊到了raid5的话题,我和他的意见产生了分歧.他的说法是raid5不能挂太多盘是因为如果挂太多盘写惩罚会非常严重导致性能下降.而我的观点则是对于ra ...