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

How to avoid automatic updating of entities acquired by get/load method in Hibernate after calling set method

2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

How to avoid the automatic update of the entity obtained by get/load method in Hibernate after calling set method? I believe that many inexperienced people are at a loss about this. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

1. Description of problem symptoms

Recently, in dealing with a new requirement, the general logic of the code is to get an entity object, call the object's set method to set some of the fields, and then use the modified entity as a parameter for other places to call, based on the return value to decide whether to update the entity to the database.

According to this idea, the getByid method in the system is called, and it is found that no matter what the return value is, the entity is eventually updated to the database. Well, this is obviously a problem. [code without problems is not good code-- | |]

2. Analysis of the causes of the problem and solutions

2.1 looking at the log information, it is found that a update statement is always printed out by the system. This indicates that the system did perform an update operation, but I did not call any methods related to update.

The trace code discovers that the getById method actually calls the get method of hibernate. Well, sure enough, that's the problem.

2.3 the entities queried by Hibernate's get and load methods are persistent objects. After getting the object, if you call the object's set method, Hibernate will automatically update the value you set to the database when the transaction is submitted.

Solution:

After getting the entity object, call the getHibernateTemplate (). Evict (entity) method, which turns the persisted object into a managed state. Once in a managed state, Hibernate will no longer update the entity automatically.

3. Expansion of related knowledge

Several entity states of Hibernate:

1. Transient: after an entity is created by the new operator, it does not establish a relationship with Hibernate's Session, nor does it manually assign the entity's persistence identity (which can be considered the primary key of the mapping table).

At this point, updates to any attributes in the entity are not reflected in the database table.

two。 Persistence: when an entity creates a relationship with the Session of Hibernate, acquires the persistence identity, and exists during the Session life cycle of the Hibernate. At this point, any change to the attribute of the entity will directly affect the update of the corresponding field of a record in the database table, that is, synchronizing with the database table.

3. Out of control: when an entity creates a relationship with the Session of Hibernate and acquires the persistence identity, and the Session life cycle of the Hibernate ends, the persistence identity of the entity remains unchanged. Any changes to the attributes of the entity are not reflected in the database table in a timely manner.

Turning off session can change the entity from a persistent state to a managed state.

After reading the above, do you know how to avoid automatically updating the entities acquired by the get/load method in Hibernate after calling the set method? If you want to learn more skills or 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

Wechat

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

12
Report