Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Semantics are unclear for eager joins #485

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

Semantics are unclear for eager joins #485

ponzao opened this issue Aug 27, 2013 · 3 comments
Assignees
Milestone

Comments

@ponzao
Copy link
Contributor

@ponzao 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
Copy link
Member

@timowest 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
Copy link
Member

@timowest 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
Copy link
Member

@timowest 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
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants