Provided by hibernateHql statementThe batch update and delete syntaxes are also supported.
 
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 form clause, the from keyword is optional, that is, the from keyword can be left blank.
 
② There can be only one class name in the form clause, and the class name cannot have aliases.
 
③ 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 hql clause.
 
If you want to change the name attribute of a user-class instance in batches, you can use the following code snippet:
 
 
Transaction TXT = session. begintransaction (); string hqlupdate = "update user set name =: newname"; int updatedentities = session. createquery (hqlupdate ). setstring ("newname", "new name" cmd.exe cuteupdate (); TXT. commit (); Session. close (); 
The code above shows that this syntax is very similar to the executeupdate () syntax of preparedstatement. In fact, this batch update of hql directly draws on the update Statement of SQL syntax.
 
Execute a delete statement.Query.exe cuteupdate ()Method. The following is a code snippet for deleting all the above records:
 
 
Transaction txt=session.beginTransaction();String hqlDelete="delete User";int daletedEntities=session.createQuery(hqlDelete).executeUpdate();txt.commit();session.close();
 
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.