Detailed explanation of Hibernate + jdbc implementation of batch insertion, update and deletion
This paper describes the method of batch insertion, update and deletion with hibernate JDBC. Share with you for your reference, as follows:
1、 Batch insert (two methods)
1. Cache via hibernate
If you write code like this for batch insertion (initial assumption):
If the amount of data is too large, there may be an exception of memory overflow;
Tips:
(1). Hibernate L1 cache has no limit on its capacity and is used forcibly. Since all objects are saved in this cache, memory overflow will always occur when the memory reaches a certain number; (2). Hibernate L2 cache can be configured in size;
To solve the problem of memory overflow, you should regularly brush the data in the session cache to the database, and the correct batch insertion method:
(1). Set the batch size (bloggers still don't understand the difference between the following attribute and the flush () method)
The reason for configuring this parameter is to read the database as little as possible. The larger the parameter value, the fewer times to read the database and the faster the speed; In the above configuration, hibernate is submitted in batches after the program has accumulated 100 SQL;
(2). Turn off L2 cache (this blogger doesn't quite understand)
In addition to the session level L1 cache, hibernate also has a sessionfactory level L2 cache. If L2 cache is enabled, in terms of mechanism, in order to maintain L2 cache, hibernate will include objects into L2 cache during batch insertion, which will cause great performance loss and may cause exceptions, Therefore, it is best to turn off the L2 cache at the sessionfactory level;
(3). After the first and second settings are completed, clear the session level L1 cache;
2. Bypass hibernate and call JDBC API directly
Note: execute SQL statements through the Preparedstatement interface in the JDBC API. The data involved in the SQL statement will not be loaded into the session cache, so it will not occupy memory space. Therefore, the efficiency of batch insertion by directly calling the JDBC API is higher than that of Hibernate cache;
Update & & delete
Syntax format: (HQL)
update | delete from? < ClassName> [where where_conditions]
1> In the from clause, the from keyword is optional, that is, the from keyword can not be written at all. 2 > in the from clause, there can only be one class name, and the alias can be specified after the class name. 3 > connections cannot be used in batch HQL statements, neither explicit nor implicit, but sub queries can be used in the where clause. 4 > the whole where clause is optional, The syntax of where clause is the same as that of where clause in SQL statement 5 > query The executeupdate () method returns an integer value, which is the number of records affected by this operation. Since the underlying operation of Hibernate is actually completed by JDBC, if a batch update or delete operation is converted into multiple update or delete statements (association or inheritance mapping), this method can only return the number of record lines affected by the last SQL statement, Not all record lines, please note;
2、 Batch update (two methods)
1. Batch update directly using hibernate
(1) Method 1: (hibernate HQL directly supports the batch update syntax of update / delete)
(2) Mode 2: (strongly not recommended)
In this way, although batch updates can be performed, the effect is very poor and the execution efficiency is not high. You need to perform data query first, and then perform data update. Moreover, this update will be line by line, that is, an update statement will be executed for each line of records updated, and the performance is very low;
2. Bypass hibernate and call JDBC API
(1) Mode 1:
(2) Mode 2:
3、 Batch deletion (two methods)
1. Batch deletion directly using hibernate
(1) Method 1: (hibernate HQL directly supports the batch update syntax of update / delete)
(2) Mode 2: (strongly not recommended)
In this way, although batch deletion can be performed, the effect is very poor and the execution efficiency is not high. Data query needs to be performed first, and then data deletion. Moreover, this deletion will be line by line, that is, a delete statement needs to be executed for each row of records deleted, and the performance is very low;
2. Bypass hibernate and call JDBC API
(1) Mode 1:
2) Mode 2:
I hope this article will be helpful to your Java programming based on Hibernate.