Batch update and delete In hibernate

Source: Internet
Author: User

Batch update refers to updating a large volume of data in a transaction, and batch Delete refers to deleting a large volume of data in a transaction. Below Program Directly use the hibernate API to batch update the age fields of all records whose age is greater than zero in the MERs table: if the customers table contains 10 thousand records whose age is greater than zero, the find () the method loads 10 thousand customer objects to the memory. When the Tx. Commit () method is executed, the cache is cleared. hibernate executes 10 thousand update statements for updating the MERs table:
View plain Copy to clipboard Print ?
Update MERs set age =? .... Where id = I; update customers set age =? .... Where id = J ;...... Update MERs set age =? .... Where id = K; update customers set age =? .... Where id = I; update customers set age =? .... Where id = J ;...... Update MERs set age =? .... Where id = K;
The above batch update methods have two disadvantages: (1) the system occupies a large amount of memory and must first load 10 thousand customer objects to the memory and then update them one by one.
(2) The number of update statements executed is too large. Each update statement can only update one customer object. Only 10 thousand update statements can be used to update 10 thousand customer objects, which frequently accesses the database, will greatly reduce the performance of the application. To quickly release the memory occupied by 10 thousand customer objects, you can call the evict () method of the session to immediately release each customer object after updating it:
View plain Copy to clipboard Print ?
Tx = session. begintransaction (); iterator MERs = session. find ("from customer C Where C. age> 0 "). iterator (); While (MERS MERs. hasnext () {customer = (customer) MERs. next (); customer. setage (customer. getage () + 1 ); Session. flush (); Session. evict (customer);} Tx. commit (); Session. close (); Tx = session. begintransaction (); iterator MERs = session. find ("from customer C Where C. age> 0 "). iterator (); While (MERS MERs. hasnext () {customer = (customer) MERs. next (); customer. setage (customer. getage () + 1); Session. flush (); Session. evict (customer);} Tx. commit (); Session. close ();
After the age attribute of a customer object is modified in the preceding program, the flush () method and evict () method of the session are called immediately () the method enables hibernate to immediately update the database based on the state changes of the customer object, and then immediately execute the related update statement. The evict () method is used to clear the customer object from the cache, so as to release the memory occupied by it in time. However, the evict () method can only slightly improve the performance of batch operations. No matter whether the evict () method is used or not, Hibernate must execute 10 thousand update statements to update 10 thousand customer objects, this is an important factor affecting the performance of batch operations. If hibernate can directly execute the following SQL statement: Update MERs set age = age + 1 where age> 0, then the above update statement can update 10 thousand records in the customers table. However, Hibernate does not directly provide an interface for executing such update statements. The application must bypass the hibernate API and use the jdbc api to execute the SQL statement:
View plain Copy to clipboard Print ?
Tx = session. begintransaction (); connection con = session. connection (); preparedstatement stmt = con. preparestatement ("Update MERs set age = age + 1" + "where age> 0"); stmt.exe cuteupdate (); Tx. commit (); Tx = session. begintransaction (); connection con = session. connection () reparedstatement stmt = con. preparestatement ("Update MERs set age = age + 1" + "where age> 0" before using stmt.exe cuteupdate (); Tx. commit ();

The above program demonstrates the process of directly accessing the database through the jdbc api by bypassing the hibernate API. The application obtains the database connection used by the session through the connection () method of the session, creates the preparedstatement object through it, and executes the SQL statement. It is worth noting that the application still declares the transaction boundary through the hibernate transaction interface.

This article is transferred from www.35java.com 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.