Semantics are unclear for eager joins #485

Closed
ponzao opened this Issue Aug 27, 2013 · 3 comments

Comments

Projects
None yet
2 participants
@ponzao
Contributor

ponzao commented Aug 27, 2013

Adding the three leftJoin -clauses gives me more results than I actually expect. If I replace them with join -clauses I get nothing.

@Override
public List<DocumentNote> getOfDocument(Long docId) {
    return from(documentNote)
        .where(
            documentNote.document.id.eq(docId),
            documentNote.deleted.eq(false))
        .orderBy(documentNote.position.asc())
        .leftJoin(documentNote.note.allEditors).fetch()
        .leftJoin(documentNote.note.comments).fetch()
        .leftJoin(documentNote.note.types).fetch()
        .list(documentNote);
}

@ghost ghost assigned timowest Aug 27, 2013

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Aug 29, 2013

Member

According to the Hibernate docs:

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. See Section 20.1, “Fetching strategies” for more information.

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. The only reason you might need an alias is if you are recursively join fetching a further collection:

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.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 not 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.

http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch16.html#queryhql-joins

Member

timowest commented Aug 29, 2013

According to the Hibernate docs:

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. See Section 20.1, “Fetching strategies” for more information.

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. The only reason you might need an alias is if you are recursively join fetching a further collection:

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.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 not 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.

http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html/ch16.html#queryhql-joins

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Aug 29, 2013

Member

In general inner join fetch will restrict your result set if no associations are available and collection joins might add duplicate results.

I will mention this in the javadocs.

Member

timowest commented Aug 29, 2013

In general inner join fetch will restrict your result set if no associations are available and collection joins might add duplicate results.

I will mention this in the javadocs.

timowest added a commit that referenced this issue Aug 29, 2013

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 20, 2013

Member

Released in 3.2.4

Member

timowest commented Oct 20, 2013

Released in 3.2.4

@timowest timowest closed this Oct 20, 2013

@timowest timowest added this to the 3.2.4 milestone Apr 13, 2014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment