Java programmers from stupid birds to cainiao (Sixty-three) detailed explanation of hibernate (fourteen) Three Retrieval Methods

Source: Internet
Author: User


There are three types of hibernate retrieval mechanisms, which have their own advantages and disadvantages. They are divided into the following three types:

1. Search policy now

2. Delayed retrieval Policy

3. left Outer Join search policy

Load now: First, let's take a look at loading now

List customerLists=session.createQuery("from Customer as c").list(); 

When running the preceding method, Hibernate first queries all records in the MERs table, and then queries records with reference relationships in the orders table based on the ID of each record, hibernate runs the following SELECT statement in sequence:

select * from CUSTOMERS; select * from ORDERS where CUSTOMER_ID=1; select * from ORDERS where CUSTOMER_ID=2; select * from ORDERS where CUSTOMER_ID=3; select * from ORDERS where CUSTOMER_ID=4; 

Disadvantages of instant retrieval:

The number of select statements is too large. Frequent database access is required, which affects the retrieval performance. To query n customer objects, you must execute n + 1 SELECT query statement. This search policy does not use the SQL connection query function. For example, the preceding five select statements can be completed using one of the following select statements:

select * from CUSTOMERS left outer join ORDERS on CUSTOMERS.ID=ORDERS.CUSTOMER_ID 

The preceding SELECT statement uses the SQL left outer join query function to query all records of the MERs table and matching records of the orders table in a select statement. When the application logic only needs to access the customer object, but does not need to access the order object, loading the order object is totally redundant. These redundant order objects waste a lot of memory space.

Delayed loading:Let's take a look at the delayed loading.

One-to-multiple. For <set> elements, the latency search policy should be prioritized:

<set name="orders" inverse="true" lazy="true" > 


Run:

Customer customer=(Customer)session.get (Customer.class,new Long(1)); 

To retrieve only the customer object immediately, run the following SELECT statement:

Select * from MERs where id = 1;

The orders variable of the customer object references the set proxy class instance. When the application accesses it for the first time, such as calling customer. getorders (). when the iterator () method is used, Hibernate will initialize this set proxy class instance. During the initialization process, it will retrieve all order objects associated with the customer in the database and execute the following SELECT statement:

Select * from orders where customer_id = 1;

Access a collection proxy instance in the uninitialized free status

Session session = sessionfactory. opensession (); Tx = session. begintransaction (); customer = (customer) session. get (customer. class, new long (1); Tx. commit (); Session. close (); // throw an exception iterator orderiterator = customer. getorders (). iterator (); when the above code is executed, the following exception will be thrown: Error lazyinitializer: 63-exception initializing proxy

Advantages

It is up to the application to determine which objects need to be loaded, so as to avoid executing redundant select statements and loading objects that the application does not need to access. Therefore, the retrieval performance can be improved and the memory space can be saved.

Disadvantages 

If an application wants to access a proxy instance in the Free State, it must be initialized during persistence.

Applicability

One-to-many or many-to-many Association.

Objects that the application does not need to access immediately or are not accessible at all.

Left Outer Join search policy

By default, multiple-to-one join uses the left Outer Join retrieval policy.

If you set the outer-join attribute of the <allow-to-one> element of the order. HBM. xml file to true, the left Outer Join retrieval policy is always used.

• The following program code:

Order order=(Order)session.get(Order.class,new Long(1)); 

When running the session. Get () method, Hibernate executes the following SELECT statement:

select * from ORDERS left outer join CUSTOMERS on ORDERS.CUSTOMER_ID=CUSTOMERS.ID where ORDERS.ID=1 

Advantages of left outer join query

1. The application is completely transparent. no matter whether the object is in a persistent or free state, the application can easily navigate from an object to the object associated with it.

2. When an external connection is used, the number of select statements is small.

Disadvantages of left outer join query

1. objects that the application does not need to access may be loaded, which wastes a lot of memory space.

2. Complex Database Table connections also affect retrieval performance.

Applicability of left outer join query

1. Multiple-to-one or one-to-one association.

2. The object that the application needs to access immediately.

3. The database system has good table connection performance.

The retrieval policy set in the ing file is fixed, either for delayed retrieval, or for instant retrieval, or for external connection retrieval. However, the application logic is diverse. In some cases, you need to delay retrieval, and in some cases, you need to retrieve external connections. Hibernate allows you to overwrite the retrieval policy set in the ing file in the application, and the application determines the depth of the retrieval object graph at runtime.

The following session methods are used to retrieve the customer object whose oid is 1:

session.createQuery("from Customer as c where c.id=1"); session.createQuery("from Customer as c left join fetch c.orders where c.id=1"); 

• When executing the first method, the retrieval policy configured in the ing file will be used.

• When executing the second method, the hql statement explicitly specifies the left outer join to retrieve the associated order object, so it overwrites the search policy configured in the ing file. Whether the lazy attribute of the <set> element in the customer. HBM. xml file is true or false, Hibernate will execute the following SELECT statement:

select * from CUSTOMERS left outer join ORDERS on CUSTOMERS.ID =ORDERS.CUSTOMER_ID where CUSTOMERS.ID=1; 

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.