今天做了个测试,写了个测试用例来看看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的区别的更多相关文章

  1. Hibernate update 和 merge 、saveOrUpdate的区别

    this.getSession().update(obj); this.getSession().merge(obj); this.getSession().saveOrUpdate(obj); 1. ...

  2. Hibernate的merge与update方法的区别

    今天做了个测试,写了个测试用例来看看merge与update时控制台打印出来的日志有什么不一样.实体bean很简单,就id和name两个字段,接下来分别给出以下几种测试情形的控制台日志内容: 1. 数 ...

  3. hibernate中load,get;find,iterator;merge,saveOrUpdate,lock的区别

    hibernate中load,get;find,iterator;merge,saveOrUpdate,lock的区别 转自http://www.blogjava.net/bnlovebn/archi ...

  4. Hibernate: save, persist, update, merge, saveOrUpdate[z]

    [z]https://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate 1. Introduction In this ...

  5. 【JAVA】FOR UPDATE 和 FOR UPDATE NOWAIT 区别 (转)

    1.for update 和 for update nowait 的区别:首先一点,如果只是select 的话,Oracle是不会加任何锁的,也就是Oracle对 select 读到的数据不会有任何限 ...

  6. [git]merge和rebase的区别

    前言 我从用git就一直用rebase,但是新的公司需要用merge命令,我不是很明白,所以查了一些资料,总结了下面的内容,如果有什么不妥的地方,还望指正,我一定虚心学习. merge和rebase ...

  7. pod install 和 pod update的区别

    pod install 和 pod update的区别 pod install(下载并安装pod) 1,当pod file文件中有“增加pod,删除pod,修改pod”的操作之后使用. 2,pod i ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Spring Boot/Spring Cloud、ESB、Dubbo

    如何使用Spring Boot/Spring Cloud 实现微服务应用spring Cloud是一个基于Spring Boot实现的云应用开发工具,它为基于JVM的云应用开发中的配置管理.服务发现. ...

  2. [数据结构与算法] : AVL树

    头文件 typedef int ElementType; #ifndef _AVLTREE_H_ #define _AVLTREE_H_ struct AvlNode; typedef struct ...

  3. hosts,No web site is configured at this address.

    解决办法: IIS管理器->右击网站->属性->网站——>IP地址一项->选择全部未分配-> 确定关闭 问题解决.

  4. numpy里的randn

    这个函数的作用就是从标准正态分布中返回一个或多个样本值.什么是标准正态分布 来源:http://www.360doc.com/content/17/0306/13/32342759_634411464 ...

  5. [UE4]让AI跑起来

    让AI由静止状态变成跑步状态,做法跟玩家角色走路一样. 一.创建1D混合动画 二.在AI角色关联的动画蓝图中使用第一步创建的混合动画

  6. [UE4]C++实现动态加载UObject:StaticLoadObject();以Texture和Material为例

    相关内容: C++实现动态加载的问题:LoadClass<T>()和LoadObject<T>() http://aigo.iteye.com/blog/2281558C++静 ...

  7. php如何判断IP为有效IP地址

    不需要正则表达式来判断,因为在php5.2.0之后,有专门的函数来做这个判断了. 判断是否是合法IP if(filter_var($ip, FILTER_VALIDATE_IP)) { // it's ...

  8. xsl如何实现递归复制?

    <xsl:template match="*" mode="addSeatSelectionToAirProduct"> <xsl:eleme ...

  9. Delphi Webbrowser使用方法详解(一)

    1.webbroser介绍 该组件是一个浏览器组件,可以显示一个指定地址的网页.设置网页打开时的主页以及对网页进行相关的操作,同时也可以对HTML文件进行剪切.复制.粘贴.删除等操作.该 组件在Int ...

  10. laravel 5.3 ——路由(资源,别名)

    laravel的路由定义中,其中route:resoure(),可以直接定义类似restful风格的URL 例如:Route::resource('system/role','System\RoleC ...