-->
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: Criteria with joining 3 tables
PostPosted: Mon Sep 01, 2008 10:31 pm 
Newbie

Joined: Thu Aug 07, 2008 9:55 am
Posts: 3
Code:
    __________       ________
   |  Topic   |     |  User  | 
   |__________|     |________|
   |          |     |        |
   |          |     |        |
   |  Userid  |---->|*Userid |
   |*Topicid  |     |        |
   |__________|     |________|
               \
                \      _________
                 \    | Post    |
                  \   |_________|
                   \  | postid  |
                    ->|*Topicid |
                      |         |
                      |_________|




Code:
public class Topic{

   private int topicid;
   private User poster;
   private Post lastPost;

/* getters and setters */
}


ok what i need to do is this..
get a list of topics and within this list there should be a User object and and an object of type Post that stores the last Post in the Topic.

note: each topic could have many posts, 10, 20 100.. etc, I just need the last post by date a single post not a Set. I can get that by using the [lastPostDate] column in the post table, its and SQL Date type.

I need to do that in 1 single createCriteria(Topic.class)
the problem with the post table is that i get many rowes back so i need to filter it Order it by the [lastPostDate] and limiting the result to 1.

I think I need to do three Join tables to get all those data in one SELECT statement.

for now my code looks somthing like this
Code:
Session session = HiberUtil.getCurrentSession();
          Criteria topic = session.createCriteria(Topic.class);
          topic.setFetchMode("poster", FetchMode.JOIN)
                  .addOrder(Order.desc(orderBy))
                  .add(Restrictions.eq("forumid",forumid))
                  .setFirstResult(0)
                  .setMaxResults(maxResult);


can this be done with the Criteria API?
if yes how, if no what's the best way to do it?
i'm not that good with DB stuff :)

Many thanks

p.s sorry for my bad english

Using Hibernate version: 3.2


Top
 Profile  
 
 Post subject: Re: Criteria with joining 3 tables
PostPosted: Tue Feb 07, 2012 5:46 am 
Newbie

Joined: Wed Jan 25, 2012 1:59 am
Posts: 8
Hello
Sorry for waking dead threads as this is a old one.But I am facing the same problem of fetching data from multiple tables.
Forum is not helping me much.

If you a solution please help.Posted my problem here.

https://forum.hibernate.org/viewtopic.php?f=1&t=1014372

Regards
Sachin


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.