HibernateOperate databases in an object-oriented way.ProgramTo operate the Persistent object in the object-oriented way, it will be automatically converted into operations on the database. However, if we want to update100000Records, whether to load them one by one100000Records, and then callsSetterMethod. This method is too cumbersome and seriously affects the data access performance.HibernateThe batch processing solution provided to solve this problem.
1. Batch insert
If you want to insert100000Record, passHibernateThe following methods may be used:
1 Session session = Hibernateutil. getsession (); 2 Transaction Tx =Session. begintransaction (); 3 For ( Int I = 0; I <100000; I ++ ){ 4 User user = New User (); 5 User. setname ("username" + I ); 6 User. setage (I ); 7 Session. Save (session ); 8 } 9 TX. Commit (); 10 Session. Close ();
however, this program has a problem: when the program runs somewhere, it always throws outofmemoryexception memory overflow exception. This is because hibernate session hold a required level-1 cache, all User all instances session The reason why the level cache area is cached.
If You Want To Perform Batch Processing and achieve an ideal performance, use jdbc batch ( batching ) is crucial. jdbc batch capture quantity ( batch size ) set the parameter to an appropriate value .
1 <PropertyName= "JDBC. batch_size">20</Property>
You may also want to disable the second-level cache:
1Hibernate. cache. use_second_level_cache false
But this is not necessary.
The solution to this problem is:SessionCache data is flushed into the databaseCall clear () to control the size of the first-level cache. As follows:
1 Static Void Adduser () Throws Exception { 2 Session session = Hibernateutil. getsession (); 3 Transaction Tx = Session. begintransaction (); 4 // Insert 1000 records for 1000 cycles 5 For ( Int I = 0; I <100000; I ++ ){ 6 // Create a user object 7 User user = New User (); 8 User. setname ("username" + I ); 9 User. setage (I ); 10 11 // Cache user instances at the session level 12 Session. Save (User ); 13 14 // When the accumulators are multiples of 20, the session data is flushed into the database, and the session is cached. 15 If (I % 20 = 0 ){ 16 Session. Flush (); 17 Session. Clear (); 18 } 19 }
Ii. Batch update
The method described above also applies to batch update. You need to use the scroll () method to take full advantage of the benefits of server-side cursors when returning a lot of row data for queries.
1 Static Void Updateuser () Throws Exception { 2 Session session = Hibernateutil. getsession (); 3 Transaction Tx = Session. begintransaction (); 4 // Query all records in the User table. 5 Scrollableresults Re = session. createquery ("from user"). Setcachemode (cachemode. Ignore). Scroll (scrollmode. forward_only ); 6 Int Count = 1 ; 7 While (Re. Next ()){ 8 User user = (User) Re. Get (0 ); 9 // When Count = 20, flush the updated results from the session to the database. 10 User. setname ("New User:" + Count ); 11 If (++ Count % 20 = 0 ){ 12 Session. Flush (); 13 Session. Clear (); 14 } 15 } 16 TX. Commit (); 17 Session. Close (); 18 }
Although batch update can be performed in this way, the execution efficiency is not high because it needs to execute Data Query before data update can be executed. To avoid this situation, we can useDMLStyle to update data in batches.
Iii. DMLBatch style update/Delete
hibernate hibernate Query Language (hql) SQL style DML statement method.
BatchUpdate,DeleteStatement syntax format:(Update | delete) from? Entityname (where where_conditions ).
Note the following points:
1.FromClause,FromThe keyword is optional.
2.FromClause (From-clause. If the object name is an alias, any referenced attribute must be prefixed with this alias. If it is not an alias, any attribute reference with a prefix is invalid.
3. You cannot batchHqlStatementExplicit or implicit connections do not work. HoweverWhereSubquery can be used in the clause. You canWhereSubquery is used in the clause. A subquery can containJoin.
4. EntireWhereThe sub-statement is optional.
Instance: Use query.exe cuteupdate () to executeHqlUpdate statement
1 Static Void Updateuser () Throws Exception { 2 Session session = Hibernateutil. getsession (); 3 Transaction Tx = Session. begintransaction (); 4 5 // Define hql statements for batch update 6 String hql = "update user as user set user. Name =: Name" ; 7 // Execute update 8 Session. createquery (hql). setstring ("name", "name" Cmd.exe cuteupdate (); 9 10 TX. Commit (); 11 Session. Close (); 12 }
When using this batch update syntax, you usually only need to execute an SQL statement once.OfUpdateStatement to update all records that meet the conditions. However, you may need to execute multipleUpdateThis is because of inheritance ing.
Execute oneHql Delete.Query.exe cuteupdate ()Method:
1 // Batch Delete using DML Style 2 Static Void Deleteuser () Throws Exception { 3 Session session = Hibernateutil. getsession (); 4 Transaction Tx = Session. begintransaction (); 5 6 // Define hql statements for batch update 7 String hql = "delete user" ; 8 9 // Delete 10 Session.createquery(hqlw..exe cuteupdate (); 11 12 TX. Commit (); 13 Session. Close (); 14 }
The query.exe cuteupdate () method returns an integer value, which is the number of records affected by this operation.