在Session中保存,更新,saveOrUpdate,合并方法之间的差异? [英] Differences among save, update, saveOrUpdate, merge methods in Session?

查看:119
本文介绍了在Session中保存,更新,saveOrUpdate,合并方法之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Hibernate的新手,上周经历了Hibernate教程。我对Session类中的方法save,update,saveOrUpdate和merge有一些疑问。它们是:


  • 保存方法:用于将新创建的对象插入数据存储区。 (基本上这个标识符的值为0)。就像我创建一个新客户并调用保存操作一样,它会将其保存在数据存储中并生成标识符。



    这是正确的吗?如果我们调用保存已经持久化的对象不知道会发生什么?

  • 更新方法:它用于更新数据存储区中的持久对象(基本上,标识符的值将是非零值)。就像我加载一个新客户并在更新某个字段值后调用update操作一样,它将在数据存储中更新它。



    根据我的理解,它应该会因某种异常而失败,因为根据API更新是针对分离对象的。它是否正确?如果是的话,我们应该调用什么来更新同一会话中的对象(我的意思是如果对象没有分离)。另一点是:如果我们对新创建的对象调用update会发生什么?

  • saveOrUpdate方法:它会调用上述任一项基于未保存的值检查(它必须基于标识符为零或非零值进行操作,对吗?),所以如果我们有持续的客户对象,并且我们更新他的姓名并创建一个新帐户,那么saveOrUpdate将会保留



    我的理解正确吗?

  • 合并方法 strong>它会像更新一样行事,但如果持久对象具有相同的标识符已经在会话中,它将更新持久对象中的分离对象值并保存它。但如果当前没有与会话关联的持久化实例,则会从数据存储中加载持久对象,然后更新已加载持久对象中已分离对象的值,并且然后更新它。



    我还得到了吗?

    h2_lin>解决方案

你的评估完全正确。你得到它。



对于你的第一个问题,如果我记得正确的话, save 具体做一个插入。所以再次调用save会导致数据库中有另一行。



对于第二个问题, update 更新一个对象在会议中。所以如果对象在会话中会更新。如果该对象不在会话中,则应调用合并。我相信为分离的实例调用 update 会导致异常。


I am new to Hibernate and went through the Hibernate tutorial last week. I have a few doubts about methods save, update, saveOrUpdate and merge in the Session class. These are:

  • save method: it is used to insert the newly created object in the datastore. (Basically identifier value will be 0 for this). Like I create a new customer and call save operation, it will persist it in the datastore and generate the identifier.

    Is this correct? And if we call save on already persistent object not sure what will happen?

  • update method: it is used to update the already persistent object in the datastore.(Basically identifier value will be some non zero value for this). Like I load a new customer and call update operation after update of some field value, it will update it in datastore.

    As per my understanding it should fail with some exception because as per API update is for detached object. Is this correct? If yes, what should we call to update the object in the same session (I mean if object is not detached). Another point is: what will happen if we call update on newly created object?

  • saveOrUpdate method: it will call either of above based on unsaved-value checks (which it must be doing based on identifier zero or non zero value, right?) so if we have persistent customer object and we update last name of his and create a new account also, then saveOrUpdate will take care of it.

    Did I understand that correctly?

  • Merge method: it will act like update but here if a persistent object with the same identifier is already in the session it will update the detached object values in the persistent object and save it.

    But if there is no persistent instance currently associated with the session, this will load the persistent object from the datastore and then update the value of detached object in loaded persistent object and then update it.

    Did I also get that?

解决方案

You are exactly right in all of your assessments. You get it.

For your first question, if i recall correctly, save specifically does an insert. So calling save again will result in another row in the db.

For your second question, update updates an object in the session. So if the object is in the session it will update. If the object is not in the session, you should call merge. I believe calling update for a detached instance will result in an exception.

这篇关于在Session中保存,更新,saveOrUpdate,合并方法之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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