-->
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: OneToMany - filter the mapped table results
PostPosted: Tue Jun 23, 2009 2:38 am 
Newbie

Joined: Fri Jun 20, 2008 6:23 am
Posts: 6
I have two tables. Table A and B. Table A is a main table of objects and table B has statistics for table A (many records with timestamps and so on). So now I have mapped A and B with OneToMany on A side and want to filter the B results when table A gets loaded from database.

This is in class Game (table A). GameStatistics is table B.
Code:
@OneToMany(fetch=FetchType.EAGER, cascade=CascadeType.ALL)
@JoinColumn(name="gameid")
@OrderBy(value="datetime")
private Set<GameStatistics> gameStatistics;


So when I load entity Game I want to sort/filter results of GameStatistics (for example by date/timestamp).

Is it possible?


Top
 Profile  
 
 Post subject: Re: OneToMany - filter the mapped table results
PostPosted: Tue Jun 23, 2009 4:59 am 
Newbie

Joined: Tue Jun 23, 2009 3:31 am
Posts: 10
The relationship between Game & GameStatistics should be marked as LAZY. If it is eager then we will end up in loading all GameStatistics objects for a game, which may not be required in most of the cases.

Yes u can do it using Criteria.
Code:
Criteria criteria = session.createCriteria(Game.class);
criteria.createCriteria("gameStatistics","stats");
criteria.addOrder(Order.asc("stats.createDate"));


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.