Lazy loading and query of Hibernate

Source: Internet
Author: User

1: implement the proxy object for hibernate lazy loading (implemented through ASM and cglib packages)
2: Query of a single object
In short, there is a fundamental difference between get and load. In a word, Hibernate considers the load method to exist in the database and can safely use the agent to delay loading, if a problem is found during use, only exceptions can be thrown;
For the get method, Hibernate must obtain the actual data; otherwise, null is returned. The difference between the get () method and the load () method is that the second-level cache is not searched.
Generally, you can use the load method to obtain an object when setting a link for another object.
When the session closes and then accesses the object after returning the object, the system will report that the proxy cannot be initialized. You can view the class name through reflection (a proxy object is returned) [The following lazy loading is the same]
Solution: Initialize or access the attributes of an object through hibernate. initialize (object;
3: one-to-one (the impact on the improvement of system performance is not great)
By default, Hibernate does not use lazy loading to query the primary object.
Lazy loading is used to query slave objects. (constrained is equal to "true" lazy is equal to "proxy (default)/false/true" Fetch is equal to "select (default) [through two select queries, efficiency not high in join]/join ")
If the fetch configuration is join, lazy will be invalid.
4: one-to-many and many-to-many
Database Query overhead and network transmission overhead
Lazy and fetch are the same as above (lazy loads by default)
5: the ID and class used to access the database during lazy loading won't be accessed because the ID already exists in another object.
6: lazy loading of attributes [This is troublesome. It needs to be modified after compilation. It may be necessary to process large fields]
7: iterate method of query (n + 1 problem)
The working principle is as follows: first obtain all IDs and then read them from the cache according to the IDS.
When neither the level-1 cache nor the level-2 cache is available, check the database. [If there are 10 records, the database will be queried for 11 times, one of which is the query ID]
If data in the cache cannot be guaranteed, the iterate method is less efficient.
8: Criteria (condition query) and detachedcriteria (offline Condition query)
Session. createcriteria (user. Class)
Detachedcriteria query = detachedcriteria. forclass (user. Class );
Query. getexecutablecriteria (session );
9: Native Query
Query query S. createquery (from Object Name );
10: Query q s. createsqlquery ("select * from table name ")
Q. List ();
The object array is found.
Query q s. createsqlquery ("select * from table name"). addentity (user. ALSS );

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: 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.