Hibernate saveOrUpdate vs更新vs save / persist [英] Hibernate saveOrUpdate vs update vs save/persist

查看:137
本文介绍了Hibernate saveOrUpdate vs更新vs save / persist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解hibernate方法之间的细微差别



saveOrUpdate - update - save / persist



我知道网站上有一些类似的问题:



Hibernate中不同的保存方法之间的区别是什么?

save和saveOrUpdate之间的差异方法hibernate



但是读了它们,我没有注意到一个覆盖使用这些方法的所有问题的答案。我将提到我创建测试的例子:
我有一个表USER与记录:

  id |公司



1公司1

2公司2

我执行代码:

 会话session = HibernateUtil.getSessionFactory()。getCurrentSession ; 
事务tx = session.beginTransaction();

用户user1 =(用户)session.load(User.class,Integer.valueOf(1));
user1.setCompany(Company3);
用户user2 =(用户)session.load(User.class,Integer.valueOf(2));
user2.setCompany(Company4);
session.persist(user1);
session.save(user2);

tx.commit();

我在数据库中看到:

  id |公司



1公司3

2 Company4

我注意到在这种情况下 save persist c $ c> saveOrUpdate 或更新。因此我们的问题是什么是它们之间的差异什么时候 saveOrUpdate 更新必要。我是正确的与保存持久相关联的对象不更新即使使用级联

解决方案

save() ()用于在数据库中插入 实体。你在数据库中已经存在的实体上调用它们。



它们之间的主要区别是 save()是Hibernate专有的, code> persist()是一个标准的JPA方法。此外, save()保证为实体分配和返回ID,而 persist()不是。



update()用于将分离的实体附加到会话。



saveOrUpdate()用于根据实体的状态(新的或分离的)保存或更新实体。



请注意,您不需要调用会话的任何方法来修改附加的实体:执行

 用户user1 =(用户)session.load(User.class,Integer.valueOf(1)); 
user1.setCompany(Company3);

足以在数据库中更新用户1的公司。 Hibernate检测对附加实体所做的更改,并自动将它们保存在数据库中。


I am struggling to apprehend the slight differences between the hibernate methods

saveOrUpdate - update - save/persist.

I know there are some similar questions on the site:

What are the differences between the different saving methods in Hibernate?

Difference between save and saveOrUpdate method hibernate

but having read them, I did not notice an answer covering all the issues coming from using those methods in any case. I would to mention the example I have created to test: I have a table USER with the records:

id     |      company



1             Company1

2             Company2

I execute then the code:

 Session session = HibernateUtil.getSessionFactory().getCurrentSession();
 Transaction tx = session.beginTransaction();

 User user1 = (User) session.load(User.class, Integer.valueOf(1));
 user1.setCompany("Company3");
 User user2 = (User) session.load(User.class, Integer.valueOf(2));
 user2.setCompany("Company4");
 session.persist(user1);
 session.save(user2);

 tx.commit();

I see in the database:

id     |      company



 1             Company3

 2             Company4

I notice that save and persist in this case do the same task as saveOrUpdate or update.My question is therefore what is the diferrence between them and when are saveOrUpdate or update necessary. Am I right that with save or persist the associated objects are not updated even if using Cascade?

解决方案

Both save() and persist() are used to insert a new entity in the database. You're calling them on entities that already exist in the database. So they do nothing.

The main difference between them is that save() is Hibernate-proprietary, whereas persist() is a standard JPA method. Additionally, save() is guaranteed to assign and return an ID for the entity, whereas persist() is not.

update() is used to attach a detached entity to the session.

saveOrUpdate() is used to either save or update an entity depending on the state (new or detached) of the entity.

Note that you don't need to call any method of the session to modify an attached entity: doing

User user1 = (User) session.load(User.class, Integer.valueOf(1));
user1.setCompany("Company3");

is sufficient to have the company of the user 1 updated in the database. Hibernate detects the changes made on attached entities, and saves them in the database automatically.

这篇关于Hibernate saveOrUpdate vs更新vs save / persist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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