问题描述
通常我读过关于save()方法为对象生成新的标识符,并且只激发 INSERT 并保存它,但它不更新它,而saveOrUpdate()方法可能 INSERT strong>或 UPDATE 记录。但根据我的经验,在这里我可以通过示例代码更好地解释,
假设存在 A类,并且我通过
<$找到表A 中的记录p $ p> A a = getHibernateTemplate.findById(7);
现在我得到一个持久化对象,
现在我正在射击,
getHibernateTemplate.save(a);
所以它只是更新现有记录,但据我所知它应该创建新记录。
我可能错在关于certian的东西,有人可以清楚这一点吗?
保存
保存 $ c>将一个对象存储到数据库中。它将持续给定的瞬态实例,首先分配一个生成的标识符。
返回创建实体的 id 。
鉴于,
SaveOrUpdate()
$ b 调用 save )或 update()是否存在或不存在。例如,如果标识符存在, update()将被调用,否则将调用 save()。
还有很多像 persist(),merge(),saveOrUpdateCopy()。几乎所有的功能和可用性都略有不同。
更多信息,请阅读。
Normally I had read about save() method generates new identifier for object and only fire INSERT and save it, it does not update it, while saveOrUpdate() method may INSERT or UPDATE record.
But as per my experience, Here I can explains better by sample code,
Suppose there is Class A, and I find record from Table A by
A a = getHibernateTemplate.findById(7);
So now I get a persistent object,
And now I am trying to save record with save method by simply modifying some of fields,
Now I am firing,
getHibernateTemplate.save(a);
So it just update existing record, but as per my knowledge it should create new record.
I may be wrong about certian things, can someone clear about this?
save
Save method stores an object into the database. It will Persist the given transient instance, first assigning a generated identifier.It returns the id of the entity created.
Whereas,
SaveOrUpdate()
Calls either save() or update() on the basis of identifier exists or not. e.g if identifier exists, update() will be called or else save() will be called.
There are many more like persist(), merge(), saveOrUpdateCopy(). Almost all are same giving slight different functionality and usability.
For more, you can read this.What are the differences between the different saving methods in Hibernate?
这篇关于save和saveOrUpdate方法之间的区别hibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!