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