-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Multi-level join using criteria query
PostPosted: Sun Nov 19, 2006 1:56 pm 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
I am trying to confirm the syntax for a multi-level join using criteria query (all examples I have seen so far are for a single join). I am writing a forum application, where one forum contains multiple threads and one thread contains multiple posts. Given a forum id, I would like to write a criteria query which returns the forum along with all its threads and posts. This is how I have specified the query right now:

Code:
        Criteria forumCriteria = getSession()
            .createCriteria(Forum.class)
            .add(Restrictions.idEq(forumId))
            .setFetchMode("threads", FetchMode.JOIN)
            .setFetchMode("threads.posts", FetchMode.JOIN);

        return (Forum)forumCriteria.uniqueResult();


This seems to work, I am getting everything in one fetch, but not sure if setFetchMode("threads.posts", FetchMode.JOIN) is the right way to specify the eager fetch for posts.

Please help.
Naresh


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 03, 2006 7:10 pm 
Regular
Regular

Joined: Sun Sep 25, 2005 11:35 pm
Posts: 57
Can someone please confirm that setFetchMode("threads.posts", FetchMode.JOIN) is a valid way to specify left outer join in the example I posted earlier?

In the meanwhile, I tried to assemble the criteria query slightly differently - see below. I thought that this would also fetch forums, threads and posts in one hit, but it doesn't - it actually makes two hits.

Code:
Criteria forumCriteria = getSession()
    .createCriteria(Forum.class)
    .add(Restrictions.idEq(forumId))
    .setFetchMode("threads", FetchMode.JOIN);

forumCriteria
    .createCriteria("threads")
    .setFetchMode("posts", FetchMode.JOIN);

return (Forum)forumCriteria.uniqueResult();


Here are the two hits to the database:

Code:
select ...
from FORUM
    this_ inner join THREAD threadimpl1_ on this_.ID=threadimpl1_.FORUM_FK
    left outer join POST posts4_ on threadimpl1_.ID=posts4_.THREAD_FK
where this_.ID = ?
order by posts4_.THREAD_FK

select ...
from THREAD threads0_
where threads0_.FORUM_FK=?
order by threads0_.FORUM_FK


Can anyone tell why this version generates two queries? Also why is thread joined with inner join instead of outer join?

Thanks.
Naresh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.