Batch processing of data refers to the processing of large amounts of data in a transaction.
In the application layer for bulk operations, mainly in the following way:
- Through Session
- by HQL
- by statelesssession
- Through the JDBC API (you just have to use this, others as an understanding)
The Save () and update () methods of the session for batch operations will store the processed objects in their own cache. Assuming that a large number of persisted objects are handled through a Session object, you shouldpurge objects from the cache that have been processed and will not be visited in time. The detailed approach isimmediately after processing an object or a small batch object, call the Flush () method to flush the cache, and then clear the cache in the call to the clear () method
Processing by Session is subject to the following constraints:
- There is a need to set the number of JDBC single batch processing in the Hibernate configuration file, ensuring that the number of batches of SQL statements sent to the database is consistent with the Batch_size attribute
- Hibernate cannot perform BULK insert operations on the JDBC layer if the object is using the "Identity" identifier generator
- It is recommended to turn off Hibernate's level two cache when doing bulk operations
- Batch update: When making a batch update, it is obviously undesirable to assume that all objects are loaded into the Session cache and then updated in cache one by one.
Using scrollable result set Org.hibernate.ScrollableResults, the object does not actually include any objects, only cursors that are used to locate records online. Only when the program iterates through the specific elements of the Scrollableresults object does it load the corresponding object into the database.
The Org.hibernate.ScrollableResults object is returned by the scroll method of Query
Bulk Operation via HQL
Note: HQL only supports INSERT into ... Insert statement in the form of SELECT, but does not support insert INTO ... The INSERT statement in the VALUES form. Therefore, the bulk insert operation cannot be performed using HQL.
Using Statelesssession to perform bulk operations from a formal perspective, the statelesssession is similar to the use of the session. Statelesssession compared to the session, there are the following differences:
The
- Statelesssession does not have a cache, and objects that are loaded, saved, or updated by Statelesssession are in a free state. The
- Statelesssession does not interact with Hibernate's second-level cache.
- When you call the Save (), update (), or delete () method of Statelesssession, these methods run the corresponding SQL statement immediately, and do not plan to run only one SQL statement
- Statelesssession does not perform a dirty check, so after changing the properties of the Customer object, you also need to call Statelesssession's update () method to update the data in the database. The
- Statelesssession does not perform any cascading operations on the associated object.
- two times the same Statelesssession object loaded into the OID 1 customer object, resulting in a different two object memory address. The operations of the
- statelesssession can be captured by the Interceptor interceptor, but will be ignored by Hibernate's event handling system.
public class Hibernatetest {private sessionfactory sessionfactory;private Session session;private Transaction transaction; @Beforepublic void init () {Configuration configuration = new Configuration (). Configure (); Serviceregistry serviceregistry = new Serviceregistrybuilder (). Applysettings (Configuration.getproperties ()) . Buildserviceregistry (); sessionfactory = Configuration.buildsessionfactory (serviceregistry); session = Sessionfactory.opensession (); transaction = Session.begintransaction ();} @Afterpublic void Destroy () {transaction.commit (); Session.close (); Sessionfactory.close ();} @Testpublic void Testbatch () {session.dowork (new work () {@Overridepublic void execute (Connection Connection) throws SQLException {///via JDBC native API, highest efficiency, fastest!}});}}