This article analyzes in detail the usage of Hibernate to manage Session and batch operations. Share it with everyone for your reference. The specific analysis is as follows:
Hibernate manages Session
Hibernate itself provides three methods of managing Session objects ① The life cycle of the Session object is bound to the local thread ② The life cycle of the Session object is bound to the JTA transaction ③ The Hibernate delegate program manages the life cycle of the Session object
In the Hibernate configuration file, the hibernate.current_session_context_class attribute is used to specify the Session management method. Optional values include:
① thread: The life cycle of the Session object is bound to the local thread ② jta*: The life cycle of the Session object is bound to the JTA transaction ③ managed: Hibernate delegate program to manage the life cycle of the Session object
The life cycle of the Session object is bound to the local thread:
If the hibernate.current_session_context_class attribute value of the Hibernate configuration file is set to thread, Hibernate will manage the Session in a manner bound to the local thread.
Hibernate binds the Session to the local thread according to the following rules:
When a thread (thread) calls the getCurrentSession() method of the SessionFactory object for the first time, this method will create a new Session (sessionA) object, bind the object to threadA, and return the session when threadA calls the SessionFactory object again. When using the getCurrentSession() method, this method will return the sessionA object. When threadA submits the transaction associated with the sessionA object, Hibernate will automatically flush Cache the sessionA object, then commit the transaction and close the session as desired. When threadA cancels the transaction associated with the sessionA object, the sessionA object will also be automatically closed. If threadA calls the getCurrentSession() method of the SessionFactory object again, this method will create a new Session (sessionB) object and bind the object to threadA. , and return sessionB
Process data in batches
Batch processing of data refers to processing a large amount of data in one transaction and batch operations in the application layer process. The main methods are as follows:
① Through Session
② Pass HQL
③ Through StatelessSession
④ Through JDBC API----Recommended because it is the fastest
Session performs batch operations:
Session's save() and update() methods will store the processed objects in their own cache. If a Session object is used to process a large number of persistent objects, objects that have been processed and will not be accessed again should be emptied from the cache in a timely manner. The specific method is to immediately call the flush() method to refresh the cache after processing an object or a small batch of objects, and then call the clear() method to cache the cache.
Processing operations through Session are subject to the following constraints:
The number of JDBC single batch processing needs to be set in the Hibernate configuration file. It should be ensured that the number of batches of SQL statements sent to the database each time is consistent with the batch size attribute.
If the object uses the "identity" identifier generator, Hibernate cannot perform batch insert operations in JDBC.
When performing batch operations, it is recommended to turn off Hibernate's second-level cache
Code demonstration for batch inserting data:
Copy the code and the code is as follows: News news = null;
for(int i = 0; i < 10000; i++) {
news = new News();
news.setTitle("--" + i);
session.save(news);
if((i + 1) % 20 == 0) {
session.flush();
session.clear();
}
}
Batch update: When performing batch update, it is obviously not advisable to load all objects into the Session cache at once and then update them one by one in the cache.
Use the scrollable result set org.hibernate.ScrollableResults. This object does not actually contain any objects, only the cursor used to locate records online. Only when the program traverses to access a specific element of the ScrollableResults object, it will load the corresponding object in the database.
The org.hibernate.ScrollableResults object is returned by Query's scroll method
Batch operations via HQL:
Note: HQL only supports insert statements in the form of INSERT INTO...SELECT, but does not support insert statements in the form of INSERT INTO...VALUES. Therefore, batch insertion operations cannot be performed using HQL
Batch operations through StatelessSession:
Formally, the usage of StatelessSession is similar to Session. Compared with Session, StatelessSession has the following differences:
StatelessSession has no cache, and objects loaded, saved or updated through StatelessSession are in a free state.
StatelessSession does not interact with Hibernate's second-level cache. When calling StatelessSession's save(), update(), or delete() methods, these methods will immediately execute the corresponding SQL statement instead of only planning to execute one SQL statement.
StatelessSession does not perform dirty checking, so after modifying the Customer object properties, you need to call the update() method of StatelessSession to update the data in the database.
StatelessSession will not perform any cascading operations on the associated objects. The Customer object with OID 1 is loaded twice through the same StatelessSession object. The memory addresses of the two objects obtained are different.
The operations performed by StatelessSession can be captured by the Interceptor interceptor, but will be ignored by Hibernate's event processing system.
I hope this article will be helpful to everyone’s Java programming.