Some suggestions on improving Hibernate Performance

Source: Internet
Author: User

Some suggestions for improving the performance of Hibernate 1. use the one-to-least ing as little as possible. If it is best to set lazy to true, the associated sub-tables will not be queried when querying the master table, thus saving database resources. 2. when lazy is set to true, if you need to (use Criteria or DetachedCriteria) query subtable objects, a session closed exception will be thrown because after Hibernate completes the primary Table query, the session of the hibernate object is closed, which means that the Connection in JDBC is killed. Queries without a connection (Hibernate is the Session object) are illegal operations, exception information is also reported here. So what should we do? If you want to query a sub-Table object, you must set the attribute fetch to the join value when setting one-to-minus. In this case, you only need to capture sub-objects during the query, this is also a crawling policy of hibernate. 3. if lazy = "true" and fetch = "join", querying by page with QBC will bring new problems. fetch = "join" queries by left Outer join, therefore, there will be many duplicate records in the data of the primary table. In this case, you need to add a deduplication restriction to the QBC condition object. The specific code is: criteria. setResultTransformer (Criteria. DISTINCT_ROOT_ENTITY); that is, fetch is a sub-Object Service and is not suitable for query operations on the primary object. 4. for the description of, if HQL is used for query, everything is normal. You can query sub-objects without deduplication. 5. if the fetch attribute is not set when lazy = "false", the sessin is disabled during HQL query. Therefore, you need to write the keyword to capture sub-objects in HQL. For example: from main joinFetchMain. detail as detail, if the programmer does this operation, the capture policy can be adopted when obtaining the sub-object attribute. 6. when DetachedCriteria is used to piece together a query condition, DetachedCriteria is required when the condition involves attributes of sub-objects. createAlias ("B", "B") to establish a ing relationship. Otherwise, the Hibernate query exception of cocould not resolve property is reported. For example: if entity A contains set <B>, when DetachedCriteria pieces the query condition, if the condition is A property Bp1 in entity B, it should pass createAlias () create an association between Entity A and entity B, for example, criteria. createAlias ("B", "B"), add (Restrictions. eq ("B. bp1 "," test "); otherwise, no error is reported. bp1, of course, after the association is set, it will also lead to a problem, that is, the repeated problem mentioned above, because after the association is set, hibernate uses the SQL statement that Concatenates the left Outer Join, the solution here is to add the Code mentioned above to remove duplicate data.
Of course, after the association between objects is added, B is loaded, even if delayed loading is set.

This article from the "looking for his" blog, please be sure to keep this source http://xunhe.blog.51cto.com/4714945/823543

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.