Association (association) and joins (join)

Source: Internet
Author: User
Tags joins

We can also assign an alias to the associated entity, or even to all elements in a set, by using the keyword join.

From Cat as Cat 
    inner join cat.mate as mate left
    outer join Cat.kittens as Kitten
From Cat as Cat left join Cat.mate.kittens as kittens
From Formula form full join Form.parameter param

The supported connection types are drawn from ANSI SQL.

INNER JOIN (inner join)

Left OUTER join (outer join)

Right outer join (outside connection)

Full join (fully connected, not commonly used)

Statement inner join, left outer join, and right outer join can be abbreviated.

From Cat as Cat 
    join Cat.mate as mate left
    join Cat.kittens as Kitten

By using the HQL with keyword, you can provide additional join conditions.

From Cat as Cat left 
    join Cat.kittens as kitten with 
        kitten.bodyweight > 10.0

Also, a "fetch" connection allows the collection of associated objects or sets of values to be initialized with the initialization of their parent object, using just one SELECT statement, this method is especially useful in cases where the collection is used, and it effectively replaces the outer joins in the mapping file for associations and collections With the delay statement (lazy declarations).

From Cat as Cat 
    INNER JOIN fetch cat.mate left
    join fetch Cat.kittens

A fetch connection usually does not need to be aliased because the associated object should not be used in the WHERE clause (or any other clause). At the same time, the associated objects are not returned directly from the query's results, but they can be queried by their parent object.

From Cat as Cat 
    INNER JOIN fetch cat.mate left
    join fetch cat.kittens a Child left
    join fetch Child.kittens

If you use iterate () to invoke a query, note that the fetch construct is not available (scroll () can be used). The fetch should also not be shared with setmaxresults () or Setfirstresult (), because these operations are based on the result set, and may contain duplicate data when the collection class is crawled beforehand, that is, the exact number of rows cannot be known in advance. The fetch is also not available with a separate with condition. You can create a Cartesian product by fetch multiple sets in a single query, so be careful. For bag mappings, it is also important to be careful when the join fetch multiple set roles may in some cases give unexpected results. Finally, it doesn't make sense to use the full join fetch with the RIGHT join fetch.

If you use attribute-level deferred fetching (lazy fetching), you can use the fetch all property to force the hibernate to immediately obtain those attributes (in the first query) that would otherwise have been delayed for loading.

From Document fetch all properties ORDER by name
From Document doc to fetch all properties where lower (doc.name) like '%cats% '

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.