Hibernate optimization method

Source: Internet
Author: User
Tags bulk insert int size

Hibernate optimization method One: Batch modification and deletion

In Hibernate 2, if you need to modify and delete any data, you need to perform a query operation, after the data to be modified or deleted, then the corresponding operation of the data. This approach is fine in the case of small amounts of data, but the following problems may occur when you need to process a large amount of data:

Consumes a large amount of memory.

The Update/delete statement needs to be executed more than once, and only one piece of data can be processed per execution.

The presence of the above two problems will seriously affect the performance of the system. Therefore, a HQL statement for batch updating or deletion of data was introduced in Hibernate 3. This allows the developer to update or delete more than one record at a time, without having to modify or delete a record at once.

If you want to delete all the user objects (that is, the records in the table corresponding to the user object), you can use the following HQL statement directly:

Delete User

When you execute this HQL statement, you need to invoke the Executeupdate () method of the Query object, as shown in the following example:

String hql= "Delete User";

Query query=session.createquery (HQL);

int size=query.executeupdate ();

Modifying and deleting data in this way is about the same performance as using JDBC directly, and is the right way to recommend it.

If you cannot make a large amount of data changes with the HQL statement, that is, you can only use the method of removing and modifying, you will also experience a memory overflow problem in bulk inserts, so you should also use the processing method provided above for similar processing.

Hibernate optimization Method Two: Use SQL to perform bulk operations

In the case of bulk INSERT, modify, and delete operations, the SQL statement that uses JDBC directly to execute the original ecology will undoubtedly achieve optimal performance because the following processing is omitted or simplified during processing:

The conversion of the HQL statement to the SQL statement.

The initialization of the Java object.

Cache processing of Java objects.

But one of the most important issues when executing SQL statements directly using JDBC is to work with Java objects in the cache. Because this low-level modification of the data will not be able to notify the cache to perform the appropriate update operations to ensure that the objects in the cache are consistent with the data in the database.

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.