database Hibernate saveOrUpdate vs 更新 vs 保存/持久化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25708982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 07:48:04  来源:igfitidea点击:

Hibernate saveOrUpdate vs update vs save/persist

databasehibernatepersistence

提问by arjacsoh

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

我正在努力理解休眠方法之间的细微差别

saveOrUpdate - update - save/persist.

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?

Hibernate 中不同的保存方法有什么区别?

Difference between save and saveOrUpdate method hibernate

save 和 saveOrUpdate 方法休眠的区别

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:

但是在阅读它们之后,我没有注意到涵盖在任何情况下使用这些方法所产生的所有问题的答案。我想提一下我创建的用于测试的示例:我有一个包含记录的表 USER:

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 saveand persistin this case do the same task as saveOrUpdateor update.My question is therefore what is the diferrence between them and when are saveOrUpdateor updatenecessary. Am I right that with saveor persistthe associated objects are not updated even if using Cascade?

我注意到,save以及persist在这种情况下做同样的任务,saveOrUpdateupdate因此。我的问题是什么是它们之间的diferrence并当saveOrUpdate还是update必要的。即使使用withsavepersist关联的对象也不会更新,我对Cascade吗?

回答by JB Nizet

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

双方save()persist()用来插入一个新的数据库中的实体。您在数据库中已经存在的实体上调用它们。所以他们什么都不做。

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.

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

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

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

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

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

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.

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

回答by user2815238

saveSave method stores an object into the database. That means it insert an entry if the identifier doesn't exist, else it will throw error. If the primary key already present in the table, it cannot be inserted.

saveSave 方法将一个对象存储到数据库中。这意味着如果标识符不存在,它会插入一个条目,否则它会抛出错误。如果主键已经存在于表中,则无法插入。

updateUpdate method in the hibernate is used for updating the object using identifier. If the identifier is missing or doesn't exist, it will throw exception.

update休眠中的更新方法用于使用标识符更新对象。如果标识符丢失或不存在,它将抛出异常。

saveOrUpdateThis method calls save() or update() based on the operation. If the identifier exists, it will call update method else the save method will be called. saveOrUpdate() method does the following: If the object is already persistent in the current session, it do nothing If another object associated with the session has the same identifier, throw an exception to the caller If the object has no identifier property, save() the object If the object's identifier has the value assigned to a newly instantiated object, save() the object - See more at: http://www.javabeat.net/difference-between-hibernates-saveupdate-and-saveorupdate-methods/#sthash.ZwqNlWXH.dpuf

saveOrUpdate该方法根据操作调用 save() 或 update() 。如果标识符存在,它将调用更新方法,否则将调用保存方法。saveOrUpdate() 方法执行以下操作: 如果对象在当前会话中已经持久化,则不执行任何操作 如果与会话关联的另一个对象具有相同的标识符,则向调用者抛出异常 如果对象没有标识符属性,则 save( ) 对象如果对象的标识符具有分配给新实例化的对象的值,则 save() 对象 - 查看更多信息:http: //www.javabeat.net/difference-between-hibernates-saveupdate-and-saveorupdate-methods /#sthash.ZwqNlWXH.dpuf

回答by Dmytro Plekhotkin

saveOrUpdate- inserts a row if it does not exist in a database or update it if it does exist.

saveOrUpdate- 如果数据库中不存在则插入一行,如果存在则更新它。

save- always try to insert a row into a database.

save- 总是尝试在数据库中插入一行。

update- always try to update a row in a database. Example of using saveOrUpdate.

update- 总是尝试更新数据库中的一行。使用示例saveOrUpdate

Assume that you developed the program that gets the information about user visits for current day from Google Analytics and saves it into your database.

假设您开发了一个程序,该程序从 Google Analytics 获取有关当天用户访问的信息并将其保存到您的数据库中。

If there are no information about the day in your database, method saveOrUpdatewill insert the data, otherwise it will update existing data.

如果数据库中没有关于当天的信息,方法saveOrUpdate将插入数据,否则将更新现有数据。