-->
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: Using criteria API to fetch objects in link table
PostPosted: Tue Jul 24, 2007 11:01 am 
Newbie

Joined: Tue Jul 24, 2007 10:45 am
Posts: 9
Hi,

Suppose I have 3 tables:
Table A
---- id
---- Name
Table B
---- id
---- Name
Table AB
---- id
---- PK of A
---- PK of B

AB table is a link table between A and B (A and B PKs are FKs in AB).

I'm trying to use the Criteria API to get A objects from the AB link table. Is that possible? Suppose I want all A objects whose name attribute is equal to "xxx" and are associated with the B object whose id is 10?

What I managed to do is to get all AB objects that satisfy these conditions:
Code:
Criteria crit = session.createCriteria(AB.class);
crit.createCriteria("A", "a");
crit.createCriteria("B", "b");
crit.add(Restrictions.eq("a.name","xxx");
crit.add(Restrictions.eq("b.id", 10);


But what I really need is to get all A objects. What should I do?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 24, 2007 3:06 pm 
Newbie

Joined: Tue Jul 24, 2007 10:45 am
Posts: 9
Solved!
The answer lies in the use of Projections

In the example above, just add in the end of the snnipet the following call:

criteria.setProjection(Projections.property("A"));


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.