Principle of JSP hibernate data saving operation

Time:2021-9-7

Data saving, updating and deletion:
1. Session. Save() method:
The session. Save () method is used for the persistent saving of entity objects, that is, when the session. Save () method is executed, the corresponding insert SQL statement will be generated to save the data. As shown in the following code:
User user=new User();
user.setName(“zx”);
Transaction tx=session.beginTransaction();
session.save(user);
tx.commit();
When the session. Save () method is executed, hibernate will not immediately generate an insert SQL statement to save the data, but it is possible to execute the insert SQL statement when the session cache is cleaned up later. What steps will the session. Save () method perform? Please see the following summary:
1、 Look for the saved object in the internal cache of the session. If it is found, it is considered that the data has been saved (insert operation has been performed), and the entity object has been in persistent state. It is returned directly. At this time, even if the data has changed compared with the previous state, the dirty data check will determine whether the update operation is required when the transaction is committed.
2、 If the entity object implements the lifecycle interface, the onsave () method of the object to be saved is executed.
3、 If the entity object implements the validatable interface, the corresponding validate () method will be executed.
4、 If there is an interceptor object, the interceptor. Onsave () method will be executed.
5、 Construct insert SQL statement to save data.
6、 After the data is saved successfully, set the ID of the entity object as the ID of the inserted record.
7、 Put the saved entity object into hibernate’s internal cache (L1 cache). Note that hibernate will not include the saved entity object into the secondary cache, because the newly saved entity object is likely to be modified later. The frequent updating of the cache and the cost of synchronization problems exceed the benefits brought by caching the object.
8、 Finally, if the object has an associated object, the cascading object will be processed recursively.

1. Session. Update() method:
As I mentioned earlier in the entity object state transformation section, the session. Update () method can re incorporate an object in a free state into hibernate’s internal cache and become a persistent object. As shown in the following code:
Configuration cfg = new Configuration();
SessionFactory sf=cfg. configure().buildSessionFactory();
Customer customer=new Customer(“zx”,27,images);// The customer object is free
Session session=sf.openSession();

Transaction tx=session.beginTransaction();
session.save(customer);// After saving, the customer object is in a persistent state
session.flush();// After emptying the cache, the customer object is in a free state
tx.commit();
session.close();

Session session2=sf.openSession();
Transaction tx2=session2.beginTransaction();
session2.update(customer);// By calling the update () method, the customer object in the free state is transformed into the persistent state again
session2.delete(customer);// After calling the delete () method, when the cache is emptied, the customer object will be moved out of the cache, and a delete transaction will be generated in the database to delete the data record corresponding to the customer object
tx.commit();
session.close();
So what steps does this method perform? It will follow the following steps:
1、 First, the entity object to be updated will be found in the cache. If it is found, it will be returned immediately. From here, we can see that if the update () method is executed on an entity object that is already persistent, it will not have any effect.
2、 Then, when the transaction is committed for cache cleaning, the dirty data will be checked to determine the changed attributes, and then the update SQL statement will be generated to update the data.
There is a problem we want to emphasize here, that is, as long as a free object is associated with a session through the update () method, the update SQL statement will be executed regardless of whether the properties of the free entity object have changed. As shown in the following code:

Transaction tx=session.beginTransaction();
session.update(customer);
tx.commit();
session.close();
In this code, no attribute value of the customer object is modified, but an update SQL statement will also be executed. If you want to not execute the update SQL statement without changing the attribute value of the entity object, you should open the “select before update” attribute of the < class > element of the entity object and set it to “true”, which is “false” by default. Configure as follows:
<class name=”com.neusoft.entity.Customer” table=”customer” select-before-update=”true”>
If this property configuration is enabled, an SQL statement similar to the following will be executed before cleaning up the session cache:
Select * from customer where id=’1′;
Query the corresponding attribute values of all customer entities in the database, and then compare them with the attribute values in the cache one by one. If there is a change, an update operation will be generated to update the data. If there is no change, the update operation will not be performed. You should decide whether to enable this option according to the actual demand. If the attributes of the entity object do not change frequently, you should enable this option to avoid unnecessary update operations. If the attributes of entity objects change frequently, it is not necessary to turn on this option to avoid executing redundant select statements before performing the update operation.

Note: (1) when executing the session. Update() operation on a free entity object, if there is no record corresponding to this entity in the database, this operation will throw an exception.
(2) . when the session. Update () method is executed to associate a free object with a session, if a persistent object with the same oid as the entity object already exists in the cache, this method will throw an exception. As shown in the following code:
Customer customer1=new Customer(“1”,“zx”,27,images);
Session session1=sf.openSession();
Transaction tx=session1.beginTransaction();
session.save(customer1);
session.flush();
tx.commit();
session1.close();

Session session2=sf.openSession();
Transaction tx2=session2.beginTransaction();
Customer othercustomer=(Customer)session2.load(Customer.class,”1”);
session2.update(customer1)
tx2.commit();
session2.close();
When associating the free object customer1 with session2 again, because of the load () operation, an othercustomer object with the same oid as customer1 has been loaded in the cache. At this time, because the object cache mechanism of Hibernate cache does not allow caching objects with the same oid, an exception will be thrown.
2、 Session.saveOrUpdate():
This method includes the characteristics of the save () method and the update () method. If a free object is passed in, this method will perform the update operation. If a temporary object is passed in, this method will perform the insert operation. The working principle behind this method is as follows:
a) First, look in the cache. If you find the operation to be saved, you will return directly.
b) If the entity implements the interception method, execute the isunsaved () method to judge the state of the entity object.
c) If the entity is in a temporary state, execute save (). If the entity is in a free state, execute update ().
There is a problem here, that is, how does hibernate judge whether an entity is in a free state or a temporary state? If an entity satisfies one of the following conditions, it is considered to be in a temporary state.
The oid value of the. Java object is null.
. if the Java object has the version attribute (which will be explained in the concurrent locking section) and is null.
. if the < ID > attribute of the entity is set to unsaved value, and the oid value is equal to the unsaved value value.
. if the version attribute of the entity is set to unsaved value, and the value of the version attribute is equal to the unsaved value value.
. if the entity implements interceptor and the interceptor. Isunsaved() method returns true.
If one of these conditions is met, the entity is considered a temporary object.
3、 Session.delete():
The delete () method is used to delete the data corresponding to one or a batch of entities from the database. If the incoming object is a persistent object, the delete operation will be executed when cleaning up the cache. If a free object is passed in, the object will be associated with the session first, and then the delete operation will be performed when the cache is cleaned up. See the following code:
Session session=sessionFactory().openSession();
Transaction tx=session.beginTransaction();
Customer customer=(Customer)session.load(Customer.class,”1”);
session.delete(customer);// Plan to execute a delete statement
tx.commit();// Clean up the cache and execute a delete statement
session.close();// Close the session, and the customer object will be deleted from the cache.
If the customer object in the above code is a free object, when the session. Delete () method is executed, the free customer object will be associated with the session first, and then when the cache is cleaned up, the delete operation will be executed. If you want to delete multiple pieces of data at once, you can use an overloaded delete () method: Delete (“from customer C where c.id > ‘8’); This method can delete all qualified data.