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

How to solve the problem of batch update of Hibernate

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

Share

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

This article mainly introduces how to solve the Hibernate batch update problem, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

For Hibernate batch update operation, Hibernate is to check out the data that meets the requirements, and then do the update operation. The same is true of batch deletion, first check out the qualified data, and then do the deletion operation.

This has two major disadvantages:

1. Takes up a lot of memory.

two。 When dealing with massive data, the execution of update/delete statements is massive, and a update/delete statement can only operate on one object, so it is conceivable that the performance of the database is poor when operating the database frequently.

After the release of Hibernate3, bulk update/delete is introduced to the batch update / delete operation of Hibernate, which is based on the principle that the batch update / delete operation of JDBC is completed through a HQL statement, which is very similar to the batch update / delete operation of JDBC. In terms of performance, it has a great improvement over Hibernate batch update / delete.

Transaction tx=session.beginSession (); String HQL= "delete STUDENT"; Query query=session.createQuery (HQL); int size=query.executeUpdate (); tx.commit (); …

The console outputs only one delete statement Hibernate:delete from T_STUDENT, which has less execution and almost the same performance as using JDBC, which is a good way to improve performance. Of course, in order to have better performance, the author suggests that batch update and delete operations still use JDBC, the method and basic knowledge points are basically the same as the above batch insertion method 2, there is no redundancy here.

The author provides another method here, which is to consider improving performance from the database side and call the stored procedure on the Hibernate program side. Stored procedures run faster on the database side. Taking the batch update as an example, the reference code is given.

First, set up a stored procedure called batchUpdateStudent on the database side:

Transaction tx=session.beginSession (); Connection conn=session.connection (); String pd= "… {call batchUpdateStudent}"; CallableStatement cstmt=conn.PrepareCall (pd); cstmt.setInt (1m 20); / / set the age parameter to 20 tx.commit ()

If you look at the above code, you will also bypass Hibernate API and use JDBC API to call the stored procedure, using the transaction boundary of Hibernate. Stored procedure is undoubtedly a good way to improve the performance of batch processing, run directly with the database side, to some extent, transfer the pressure of batch processing to the database.

Thank you for reading this article carefully. I hope the article "how to solve the problem of batch update of Hibernate" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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