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

Example Analysis of Hibernate update Operation

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces Hibernate update operation example analysis, the article is very detailed, has a certain reference value, interested friends must read!

Before everything else, for Hibernate, its objects have three states: transient, persistent, detached.

Here are some common translations:

◆transient: transient or free state (new DeptPo(1,"administrative department", 20,"administrative related"), the instance of this po and the session are not related, the instance of this po is in transient)

◆persistent: persistent state (and Po instance to be mapped recorded in the database, its state is persistent, objects obtained by get and load are persistent)

◆detached: detached state or free state

(1)When po objects obtained by get or load methods are persistent, but if delete(po) is executed (but the transaction cannot be executed), the po state is detached, (indicating that it is disconnected from the session), and becomes free due to delete. It can be changed to persistent state by save or saveOrUpdate().

(2)When the session is closed, the persistent po object in the session cache also becomes detached.

Hibernate update and flush

Hibernate update operates on objects that are in a free or detached state (detached due to session closure)//updateSQL, while flush operates on objects that are in a persistent state.

By default, changes to a persistent object (including the set container) do not need to be updated, as long as you change the value of the object, wait for Hibernate flush to automatically update or save to the database. Hibernate flush occurs in several situations:

1, call some queries and manually flush(), close session, close SessionFactory combination. get() an object, change the properties of the object, close the resource.

2, transaction commit time (including flush)

Hibernate update and lock

Hibernate update is to change an object that has been changed from an out-of-pipe state to a persistent state

lock is to change an object that has not been changed into a persistent state (for po objects (2) that are out of tube due to the closing of Session, not for po objects that are out of tube due to delete)

Corresponding to changing the contents of one record, the operations of the two are different:

◆ The operation steps of update are: modify the out-of-tube object after the attribute change-> call update

◆ The operation steps of lock are: Call lock to change the unmodified object from the detached state to the persistent state--> Change the contents of the persistent state object--> Wait for flush or manually flush

Difference between clear and evcit

clear Clear the session cache completely. evcit(obj) empties a persistent object from the session cache.

session.lock(xtyhb,LockMode.NONE);

//means to go directly to the cache to find objects that have become persistent

session.lock(xtyhb,LockMode.READ);

//First read the ID of the record in the database to see if there is a record. If there is, then go to the cache to find the object that has become persistent.

The above is "Hibernate update operation example analysis" all the content of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

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