Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Hibernate objects have several states

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Editor to share with you about the several states of Hibernate objects. I hope you will gain something after reading this article. Let's discuss it together.

one。 Preliminary knowledge:

Before all this, note that Hibernate objects have three states: transient, persistent, and detached

The following are common translation methods:

◆ transient: transient or free state

◆ persistent: persistence status

◆ detached: out-of-tube or free state

1. Instances of unmanaged state can be persisted by calling the save (), persist (), or saveOrUpdate () methods.

two。 Persistent instances can become out-of-pipe by calling delete (). The instances obtained through the get () or load () methods are persistent.

3. Instances of unmanaged state can be persisted by calling update (), 0saveOrUpdate (), lock (), or replicate ().

Save () and persist () will raise SQL's INSERT,delete () will raise SQLDELETE, while update () or merge () will raise SQLUPDATE. Changes to persistent (persistent) instances are detected when the commit is refreshed, and it also causes SQLUPDATE.saveOrUpdate () or replicate () to raise SQLINSERT or UPDATE.

II. The difference between save and update

The reason for putting this pair in the * position is because this pair is the most commonly used.

The function of ◆ save is to save a new object

◆ update is to save an object in an unmanaged state

III. The difference between update and saveOrUpdate

This is easy to understand. As the name implies, saveOrUpdate is basically a combination of save and update, citing a passage in Hibernate reference to explain their usage and differences. Usually the following scenarios use update () or saveOrUpdate ():

◆ program loads objects in * session

◆ the object is passed to the presentation layer

Some changes have taken place in the ◆ object

◆ the object is returned to the business logic layer

The ◆ program calls the update () method of the second session to persist these changes.

SaveOrUpdate () does the following:

◆ if the object has been persisted in this session, do nothing

◆ throws an exception if another object associated with this session has the same persistence identity (identifier)

◆ if the object does not have a persistence identity (identifier) property, call save () on it

◆ call save () on an object if its persistent identity (identifier) indicates that it is a newly instantiated object

◆ if the object is accompanied by version information (via or) and the value of the version property indicates that it is a newly instantiated object, save () it. Otherwise, the object update ().

After reading this article, I believe you have a certain understanding of "several states of Hibernate objects". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report