Associations and joins in Hibernate by R4R Team

Associations and joins in Hibernate: For the use of Join wee can assign associated entities or elements of a collection of values. 

For example:

    from Student as student inner join student.mate as mate left outer join student.kittens as kitten

    from Student as student left join cat.mate.kittens as kittens

    from Formula form full join form.parameter param

Which type support to the join types are borrowed from ANSI SQL:

   1.  inner join

    2. left outer join

    3. right outer join

    4. full join (not usually useful)

Inner join, left outer join and right outer join constructs may be abbreviated.

    from Student as student join student.mate as mate left join student.kittens as kitten

We can supply extra join conditions using the HQL with keyword.

    from Student as student left join student.kittens as kitten with kitten.bodyWeight > 10.0

A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections.

    from Student as student inner join fetch student.mate left join fetch student.kittens

A fetch join does not usually need to assign an alias, because the associated objects should not be used in the where clause (or any other clause). The associated objects are also not returned directly in the query results. Instead, they may be accessed via the parent object. 

For the only reason you might need an alias is if you are recursively join fetching a further collection:

    from Student as student inner join fetch student.mate left join fetch student.kittens child left join             fetch child.kittens

The fetch construct cannot be used in queries called using iterate() (though scroll() can be used). Fetch should be used together with setMaxResults() or setFirstResult(), as these operations are based on the result rows which usually contain duplicates for eager collection fetching, hence, the number of rows is not what you would expect. 

Fetch should also not be used together with impromptu with condition. It is possible to create a cartesian product by join fetching more than one collection in a query, so take care in this case. Join fetching multiple collection roles can produce unexpected results for bag mappings, so user discretion is advised when formulating queries in this case. Finally, note that full join fetch and right join fetch are not meaningful.

If you are using property-level lazy fetching (with bytecode instrumentation), it is possible to force Hibernate to fetch the lazy properties in the first query immediately using fetch all properties.

from Document fetch all properties order by name
from Document doc fetch all properties where lower( like '%student%'
Leave a Comment:
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!