Avoid automatic update after calling the set method of the entity obtained by the get/load method in Hibernate

 

1. Description of the symptoms of the problem

      Recently, I am dealing with a new demand problem. The general logic of the code is to obtain an entity object, call the set method of the object to set some fields, and then use the modified entity as a parameter for other places to call, and decide whether to use the return value. Update this entity to the database .

     According to this idea, the getByid method in the system is called, and when the result is tested , it is found that no matter what the return value is, the entity is finally updated to the database . Well, this is clearly buggy.... [Code without problems is not good code - -|| ]

2. Problem analysis and solution

        2.1 After viewing the log information, it is found that the system always prints an update statement. It shows that the system has indeed performed the update operation, but I did not call any methods related to update.

        2.2 The tracking code found that the getById method actually calls the get method of hibernate . Well, sure enough, here'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 set method of the object, then when the transaction is submitted, Hibernate will automatically update the value you set to in the database.

    Solution:

        After obtaining the entity object, call the getHibernateTemplate().evict(entity) method. The function of this method is to turn the persistent object into a managed state. After becoming managed, Hibernate will no longer automatically update the entity.

3. Relevant knowledge expansion

Several entity states of Hibernate:

1. Transient:
 After an entity is created through the new operator, it does not establish a relationship with the Hibernate Session, nor does it manually assign the persistent identifier of the entity (the persistent identifier can be considered as the primary key of the mapping table).
               Updates to any properties in the entity at this time will not be reflected in the database tables.
2. Persistence:
               When an entity creates a relationship with Hibernate's Session, and obtains a persistent identifier, it exists in the Hibernate's Session life cycle.
               At this time, 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, synchronization with the database table.
3. Disconnection:
              When an entity creates a relationship with Hibernate's Session and obtains a persistent identifier, and at this time, Hibernate's Session life cycle ends, the entity's persistent identifier has not been changed.
              Modifications to any attributes of the entity will not be reflected in the database tables in a timely manner.

              Closing the session can move the entity from a persistent state to a managed state.

 

 

http://blog.csdn.net/likekobe2012/article/details/72831437?locationNum=5&fps=1

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326164862&siteId=291194637