6.3 hibernate batch processing: (6.3.3) Batch update and deletion of DML styles

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.