Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the method of batch update and deletion of Hibernate?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what is the method of batch update and deletion of Hibernate". In daily operation, I believe that many people have doubts about the method of batch update and deletion of Hibernate. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "what is the method of batch update and batch deletion of Hibernate?" Next, please follow the editor to study!

Hibernate batch update refers to updating large quantities of data in a transaction, while Hibernate batch deletion refers to deleting large quantities of data in a transaction. The following procedure updates the AGE field of all records older than zero in the CUSTOMERS table directly through Hibernate API:

Tx = session.beginTransaction (); Iterator customers=session.find ("from Customer c where c.age > 0"). Iterator (); while (customers.hasNext ()) {? Customer customer= (Customer) customers.next (); customer.setAge (customer.getAge () + 1);?}? tx.commit ();? session.close ()

If there are 10, 000 records older than zero in the CUSTOMERS table, the find () method of Session loads 10, 000 Customer objects into memory at once. When the tx.commit () method is executed, the cache is cleaned and Hibernate executes 10, 000 update statements that update the CUSTOMERS table:

Update CUSTOMERS set AGE=?... . Where ID=i;? update CUSTOMERS set AGE=?... . Where ID=j;? update CUSTOMERS set AGE=?... . Where ID=k

The above Hibernate batch update method has two disadvantages:

(1) it takes up a lot of memory, so 10, 000 Customer objects must be loaded into memory first, and then updated one by one.

(2) the number of update statements executed is so large that each update statement can only update one Customer object, and ten thousand Customer objects must be updated through 10, 000 update statements. Frequent access to the database will greatly reduce the performance of the application.

To quickly free the memory occupied by 10, 000 Customer objects, you can immediately free up memory after updating each Customer object by calling the evict () method of Session:

Tx = session.beginTransaction (); Iterator customers=session.find ("from Customer c where c.age > 0"). Iterator (); while (customers.hasNext ()) {Customer customer= (Customer) customers.next (); customer.setAge (customer.getAge () + 1); session.flush (); session.evict (customer);} tx.commit (); session.close ()

In the above procedure, after modifying the age property of a Customer object, the flush () method and the evict () method of Session are immediately called. The flush () method makes Hibernate update the database synchronously according to the state changes of the Customer object, thus executing the relevant update statement immediately; the evict () method is used to clear the Customer object from the cache, thus releasing the memory it occupies in a timely manner.

However, the evict () method can only slightly improve the performance of batch operations, because with or without the evict () method, Hibernate must execute 10, 000 update statements to update 10, 000 Customer objects, which is an important factor affecting the performance of batch operations. If Hibernate can execute the following SQL statement directly:

Update CUSTOMERS set AGEAGE=AGE+1 where AGE > 0

Then the above update statement can update 10, 000 records in the CUSTOMERS table. However, Hibernate does not directly provide an interface to execute this update statement. The application must bypass Hibernate API and execute the SQL statement directly through JDBC API:

Tx = session.beginTransaction (); Connection con=session.connection (); PreparedStatement stmt=con.prepareStatement ("update CUSTOMERS set AGEAGE=AGE+1" + "where AGE > 0"); stmt.executeUpdate (); tx.commit ()

The above program demonstrates the process of bypassing Hibernate API and accessing the database directly through JDBC API. The application obtains the database connection used by the Session through the connection () method of the Session, and then uses it to create the PreparedStatement object and execute the SQL statement. It is worth noting that the application still declares transaction boundaries through the Transaction interface of Hibernate.

If the underlying database, such as Oracle, supports stored procedures, you can also perform Hibernate batch updates through stored procedures. Stored procedures run directly in the database and are faster. A stored procedure named batchUpdateCustomer () can be defined in the Oracle database as follows:

Create or replace procedure batchUpdateCustomer (p_age in number) as begin update CUSTOMERS set AGEAGE=AGE+1 where AGE > pageage; end

The above stored procedure has a parameter p_age, which represents the age of the customer, and the application can call the stored procedure in the following ways:

Tx = session.beginTransaction (); Connection con=session.connection (); String procedure = "{call batchUpdateCustomer (?)}"; CallableStatement cstmt = con.prepareCall (procedure); cstmt.setInt (1); / / set the age parameter to 0 cstmt.executeUpdate (); tx.commit ()

As you can see from the above program, the application must also bypass Hibernate API and call the stored procedure directly through JDBC API.

Session's various overloaded forms of update () methods can only update one object at a time, while some overloaded forms of delete () methods allow HQL statements as parameters, such as:

Session.delete ("from Customer c where c.age > 0")

If there are 10, 000 records older than zero in the CUSTOMERS table, the above code can delete 10, 000 records. However, the delete () method of Session does not execute the following delete statement

Delete from CUSTOMERS where AGE > 0

The delete () method of Session first loads 10, 000 Customer objects into memory with the following select statement:

Select * from CUSTOMERS where AGE > 0

Next, execute 10, 000 delete statements to delete the Customer objects one by one:

Delete from CUSTOMERS where ID=i; delete from CUSTOMERS where ID=j; delete from CUSTOMERS where ID=k

Thus it can be seen that batch Hibernate updates and Hibernate batch deletions directly through Hibernate API are not recommended. Executing relevant SQL statements or calling relevant stored procedures directly through JDBC API is a * method for batch update of Hibernate and batch deletion of Hibernate, both of which have the following advantages:

(1) there is no need to load large quantities of data in the database into memory first, and then update or modify them one by one, so it does not consume a lot of memory.

(2) can update or delete a large number of data in a SQL statement.

At this point, the study on "what is the method of batch update and batch deletion of Hibernate" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report