Hibernate common Face Question summary

Source: Internet
Author: User
Tags constructor valid

1. In the database when the query speed is very slow, how to optimize?

1. Index Construction

2. Reduce the correlation between tables

3. Optimize SQL, try to make SQL quickly locate the data, do not let SQL do the full table query, should go to the index, the data large table in front

4. Simplify the query field, do not use the field, has been the control of return results, as far as possible to return a small amount of data

2. In the hibernate of multiple table query, each table to take several fields, that is, query out of the result set does not have a corresponding entity class, how to solve this problem?

Solution One, follow the object[] data to fetch the data, and then group your own bean

Solution two, for each table's bean write constructor, such as table one to find out field1,field2 two fields, then a constructor is the bean (type1 filed1,type2 field2), and then in the HQL can directly generate this bean. Please see the relevant documents, I do not say very clearly.

The difference between session.load () and Session.get ()

The Session.load/get method can read records from the database based on the specified entity class and ID and return the entity object corresponding to it. The difference is:

If a record that matches a condition is not found, the Get method returns NULL, and the load method throws a objectnotfoundexception.

The Load method returns an instance of the proxy class for an entity, and the Get method always returns the entity class directly.

The Load method can take full advantage of the existing data in the internal cache and the level two cache, while the Get method only makes data lookups in the internal cache, and if no corresponding data is found, it will bypass the level two cache and directly invoke SQL to complete the data reading.

Session the process that will pass when the entity object is loaded:

First, a level two cache is maintained in Hibernate. The first level cache is maintained by the session instance, which maintains the data of all associated entities currently in session, also known as internal caching. The second level cache exists at the sessionfactory level and is shared by all current session instances constructed by this sessionfactory. For performance reasons, avoid unnecessary database access, the session in the database query function before calling, will first query in the cache. First, in the first level cache, the entity type and id are searched, and if the first level cache lookup hits and the data status is valid, it is returned directly.

The session is then searched in the current nonexists record and null if the same query condition exists in the nonexists record. "Nonexists" records a query condition (equivalent to a list of query blacklists) in which the current session instance failed to query for valid data in all previous query operations. As a result, if an invalid query condition in the session repeats itself, the decision can be made quickly to get the best performance.

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.