为什么hibernate在SAVE之前做SELECT? [英] Why does hibernate do the SELECT before SAVE?

查看:369
本文介绍了为什么hibernate在SAVE之前做SELECT?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么hibernate在保存对象之前做了select?



我无法在Internet上找到有用的信息。这是每次保存前的正常行为吗?我发现这个话题,选择查询运行hibernateTemplate.save() - 保存,但我没有找到这个答案明确。我的意思是,如果我想在保存每个对象之前避免这种选择,我们是否必须使用版本控制?



我将不胜感激所有解释或链接。 b $ b

解决方案

我知道你在问题的评论中回答了你自己的问题,但只是总结这里的一些一般观点。
$ b

为了澄清,NHibernate在'SQL INSERT'和'update'中使用'save',就像'SQL UPDATE'一样。

我知道这些常见的场景,当NHibernate会在数据库中隐式地获取一个对象(没有明确使用s.Update),然后再坚持下去:


  1. 在映射select-before-update中设置为true时,session_flush / transaction_commit(取决于设置);
  2. 使用SaveOrUpdate并且实例的标识符具有值这表明它存在于数据库中;

  3. 之前s.Delete。

例如,这可能不会在使用父子对象时显而易见(但简单的规则保持不变),因为从代码中可能不明显,子代将被提取。

Why does hibernate do a select before saving an object?

I can't find useful information on internet. Is this normal behavior before every save? I found this topic, select Query Run for hibernateTemplate.save() - save, but I do not find this answer "definitive". I mean, do we have to use versioning if i want to avoid this select before saving each object?

I would appreciate all the explanations or links.

解决方案

I know you have answered your own question in the question's comment, but just to summarise this here are some general points.

Just to clarify, NHibernate uses 'save' as in 'SQL INSERT' and 'update' as in 'SQL UPDATE'.

I know of these common scenarios when NHibernate will fetch an object implicitly (no explicit use of s.Update) from the db before persisting it:

  1. On session_flush/transaction_commit (depending on settings) when in the mapping select-before-update is set to "true";
  2. When SaveOrUpdate is used and the identifier of the instance has a value which suggests it exists in the db;
  3. Before s.Delete.

As with your example, this may not be obvious when parent-child objects are used (but simple rules stay the same) as it may not be obvious from the code that children will be fetched.

这篇关于为什么hibernate在SAVE之前做SELECT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆