Use hibernate for batch update and delete operations

Source: Internet
Author: User
The functions in this article are only available in hibernate3. See bulkmanipulationtest. Java in the org. hibernate. Test. hql package in the source code of the hibernate3 file.

In hibernate2, the batch operation processing method is to query the primary keys of all qualified database data, and then operate on each data entry based on the primary key. It is time-consuming and laborious. The latest hibernate3 provides the batch update function. For example, the program code string hqlupdate = "update user U" +
"Set U. Username =: newusername, U. Password =: newpassword" +
"Where U. Username =: username and U. Password =: Password";
Query query = session. createquery (hqlupdate );
Query. setstring ("username", "HFM ");
Query. setstring ("password", "1 ");
Query. setstring ("newusername", "Ps ");
Query. setstring ("newpassword", "123 ");
Int num = query.exe cuteupate ();
TS. Commit (); program code string hqldelete = "delete from user U" +
"Where U. Username =: username and U. Password =: Password";
Query. setstring ("username", "Ps ");
Query. setstring ("password", "123 ");
Int num = query.exe cuteupate ();
TS. Commit ();

The variable session type is org. hibernate. Session. The ts class behavior is org. hibernate. transaction. The returned value of num indicates that several pieces of data have been operated. After the preceding two sections of code are executed, the following SQL statements are displayed on the console:

Hibernate: Update customer user0 _ set user0 _. Username = ?, User0 _. Password =? Where (user0 _. Username =? And user0 _. Password = ?)

Hibernate: delete from customer user0 _ Where (user0 _. Username =? And user0 _. Password = ?)

The above is a good method I think. However, note that the second statement cannot be executed in MySQL, but hql can still be compiled into SQL statements. I don't know whether the syntax is wrong or a bug? Who knows can mail me: lippea@sohu.com. In addition, there is a method in the reference file bulkmanipulationtest. Java:

Public querytranslatorimpl asserttranslation (string hql) throws queryexception, mappingexception;

I don't know what this method is, but I tried to Perform Batch operations in this form: program code string hqldelete = "delete from user U" +
"Where U. Username =: username and U. Password =: Password";
Object [] objects = new object [] {"HFM", "1 "}
Type [] types = new type [] {(type) hibernate. String, (type) hibernate. string };
Queryparameters queryparas = new queryparameters (types, objects );
Querytranslatorfactory ast = new astquerytranslatorfactory ();
Querytranslator newquerytranslator =
AST. createquerytranslator (hqldelete, collections. empty_map,
(Sessionfactoryimplementor) SF );
Newquerytranslator. Compile (collections. empty_map, false );
Int num = newquerytranslator.exe cuteupdate (queryparas,
(Sessionimplementor) session );

The SF type is org. hibernate. sessionfactory, and the session type is org. hibernate. Session. The results of using this method are the same as those of the above method, but it is inconvenient to assign values to variables in hql. I have not found any more useful method. Maybe there are other methods to assign values, or are they useful?

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.