merge与update区别---->你一定要看!

本文详细对比了Hibernate框架中merge与update两种方法在不同场景下的行为差异,包括数据库记录存在时的更新操作及记录不存在时的处理方式。

                         merge与update区别

注:就因为这2个方法的区别还得我花了太多时间项目迟迟不能做完

    ,但是让我解决了,学到了东西了.

这是一段代码

public void updateData(Object obj) {
  try {
   tx = this.getSession().beginTransaction();
   // 执行修改操作

  //this.getSession().update(obj);
   this.getSession().merge(obj);
   tx.commit();
  } catch (Exception e) {
   System.out.println("===修改信息出现异常===");
   e.printStackTrace();
   tx.rollback();
  }
 }

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还是托管状态。

error: Invalid value for --rebase: ebase usage: git pull [<options>] [<repository> [<refspec>...]] -v, --verbose be more verbose -q, --quiet be more quiet --progress force progress reporting Options related to merging -r, --rebase[=<false|true|preserve>] incorporate changes by rebasing rather than merging -n do not show a diffstat at the end of the merge --stat show a diffstat at the end of the merge --log[=<n>] add (at most <n>) entries from shortlog to merge commit message --squash create a single commit instead of doing a merge --commit perform a commit if the merge succeeds (default) --edit edit message before committing --ff allow fast-forward --ff-only abort if fast-forward is not possible --verify-signatures verify that the named commit has a valid GPG signature -s, --strategy <strategy> merge strategy to use -X, --strategy-option <option=value> option for selected merge strategy -S, --gpg-sign[=<key-id>] GPG sign commit Options related to fetching --all fetch from all remotes -a, --append append to .git/FETCH_HEAD instead of overwriting --upload-pack <path> path to upload pack on remote end -f, --force force overwrite of local branch -t, --tags fetch all tags and associated objects -p, --prune prune remote-tracking branches no longer on remote --recurse-submodules[=<on-demand>] control recursive fetching of submodules --dry-run dry run -k, --keep keep downloaded pack --depth <depth> deepen history of shallow clone --unshallow convert to a complete repository --update-shallow accept refs that update .git/shallow --refmap <refmap> specify fetch refmap
最新发布
08-20
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大龄牛码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值