Use of Outerjoin and fetch joins in hibernate

Source: Internet
Author: User
Tags join joins

1, outer-join Keywords (many-to-one)

The Outer-join keyword has 3 values, respectively, True,false,auto, and Auto is the default.

True: Uses an outer join to crawl the associated content, which means that when using load (Orderlineitem.class, "id"), Hibernate generates only one SQL statement to initialize the Orderlineitem with his father order.

SELECT * FROM Orderlineitem O-left joinorderpon o.orderid=p.orderid where o.orderlineitem_id=?

False: does not use an outer join to crawl the associated content, when load (Orderlineitem.class, "id"), Hibernate generates two SQL statements, one query Orderlineitem table, the other Query order table. The advantage is that you can set up deferred loading, where you set the order class to lazy=true.

SELECT * from Orderlineitem owhere o.orderlineitem_id=?

SELECT * from order p where p.orderid=?

Auto: specifically ture or false look at the configuration in Hibernate.cfg.xml

Note: If you use the HQL query Orderlineitem, such as the From Orderlineitem o where o.id= ' id ', always do not use the external crawl, and outer-join invalid.

2, Outer-join (set)

Because the collection can set lazy= "true", lazy and outer-join cannot be true at the same time, Outer-join will always be false when lazy= "true", if lazy= "false", The Outer-join usage is the same as 1

3, the HQL statement will query the association in the Pojo configuration file, even if there is no explicit join in the HQL statement.

4. In HQL, the "fetch join" clause can is used for per-query specific outer join fetching. One important thing many people miss there, is that HQL queries would ignore the Outer-join attribute you specified in your Mapping. This is makes it possible to configure the default loading behaviour of Session.load () and Session.get () and of objects Loade D by navigating relationship. So if you specify

And then do

MyObject obj = Session.createquery ("from MyObject"). Uniqueresult ();

Obj.getmyset (). iterator (). Next ();

You'll still have an additional query and no outer-join. So you must explicily request the Outer-join fetching:

MyObject obj = Session.createquery (

"From MyObject Mo left join fetch Mo.myset"). Uniqueresult ();

Obj.getmyset (). iterator (). Next ();

Another important thing to know are that you can only fetch one collection reference in a query. That means you can just the use one fetch join. Can however fetch "one" references in addition, as this sample from the Hibernate Docs demonstrates:

From eg. Cat as Cat

INNER JOIN Fetch Cat.mate

Left JOIN Fetch Cat.kittens

We have once considered lifting this limitation, but then decided against it, because using more than one fetch-join Being a bad idea generally:the generated ResultSet becomes huge and is a major performance.

So alltogether the ' fetch join ' clause is a important instrument Hibernate users should learn to leverage, as it Allo WS tuning the fetch behaviour of a certain use case.

5. The difference between join fetch and join

If HQL uses a connection but does not use the FETCH keyword, the resulting SQL statement, although connected, does not take data from the attached table, or requires separate SQL fetch data, which is the select A,b,d ... Fields with no linked tables in

6. If the collection is declared as lazy=true, delay loading is invalidated if the join fetch is explicitly used in HQL.

7. Explicitly set the fecth= "join" at the one end of the One-to-many, then take a pre-crawl (Generate one SQL), Delay load failure (generate two SQL)

8. Many-to-one delay Loading is set lazy= "true" in the Class tab of the configuration file, and One-to-many and Many-to-many delay loading is set lazy= "true" in the set label. The one-to-one does not just set lazy= "true" in the Calss tab, but also set constrained= "true" in the One-to-one tab.

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.