HQL statements provided by Hibernate also support batch UPDATE and DELETE syntax.
The syntax format of batch UPDATE and DELETE statements is as follows:
- UPDATE | DELETE FROM? <ClassName> [WHERE WHERE_CONDITIONS]
The preceding syntax format is worth noting as follows:
Ø in the from clause, the FROM keyword is optional, that is, the FROM keyword can be left blank.
The FROM clause can have only one class name. The class name cannot have an alias.
Ø Connections cannot be used in batch HQL statements, either explicitly or implicitly. However, subqueries can be used in the WHERE clause.
The entire WHERE clause is optional. The syntax of the WHERE clause is exactly the same as that of the WHERE clause in the HQL statement.
If you want to change the name attribute of a User-class instance in batches, you can use the following code snippet.
Program configuration: codes \ 06 \ 6.3 \ batchUpdate2 \ src \ lee \ UserManager. java
- Private void updateUsers () throws Exception
- {
- // Open the Session
- Session session = HibernateUtil. currentSession ();
- // Start the transaction
- Transaction tx = session. beginTransaction ();
- // Define HQL statements for batch update
- String hqlUpdate = "update User set name =: newName ";
- // Execute update
- Int updatedEntities = session. createQuery (hqlUpdate)
- . SetString ("newName", "new name ")
- . ExecuteUpdate ();
- // Submit the transaction
- Tx. commit ();
- HibernateUtil. closeSession ();
- }
From the code above, we can see that this syntax is very similar to the executeUpdate () syntax of PreparedStatement. In fact, the batch UPDATE of HQL directly references the UPDATE Statement of SQL syntax.
Note:
When using this batch UPDATE syntax, you usually only need to execute an SQL UPDATE statement to complete all updates that meet the conditions. However, you may also need to execute multiple UPDATE statements because of special circumstances such as inheritance ing. For example, there is a Person instance with a Customer subclass instance. When you update the Person instance in batches, you also need to update the Customer instance. If you use the joined-subclass or union-subclass ing policy, the Person and Customer instances are stored in different tables. Therefore, you may need multiple UPDATE statements.
Execute an hql delete statement and use the Query.exe cuteUpdate () method. The following is a code snippet that deletes all the preceding records at a time.
Program configuration: codes \ 06 \ 6.3 \ batchDelete \ src \ lee \ UserManager. java
- Private void deleteUsers () throws Exception
- {
- // Open the Session
- Session session = HibernateUtil. currentSession ();
- // Start the transaction
- Transaction tx = session. beginTransaction ();
- // Define HQL statements for batch Deletion
- String hqlDelete = "delete User ";
- // Execute Delete
- Int deletedEntities = session. createQuery (hqlDelete)
- . ExecuteUpdate ();
- // Submit the transaction
- Tx. commit ();
- HibernateUtil. closeSession ();
- }
The Query.exe cuteUpdate () method returns an integer value, which is the number of records affected by this operation. We know that the underlying operations of Hibernate are actually performed by JDBC. Therefore, if batch UPDATE or DELETE operations are converted into multiple UPDATE or DELETE statements, this method returns only the number of rows affected by the last SQL statement.
This article from the "crazy Java Li Gang" blog, please be sure to keep this source http://javaligang.blog.51cto.com/5026500/912874