-->
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.  [ 1 post ] 
Author Message
 Post subject: Criteria API - Foreign Query
PostPosted: Sat Oct 24, 2009 8:26 am 
Newbie

Joined: Sat Oct 24, 2009 8:04 am
Posts: 1
Hello there,

Model:
Code:
@Entity
class Account {
   @Id
   long id;

   @Column
   String accountNumber;

   @OneToMany
   Set<Card> cards;
}

@Entity
class Card {
   @Id
   long id;

   @Column
   String cardNumber;


How to retrieve a list of cards of given account with criteria API?
I mean an equivalent of the following HQL query
Code:
select card from Account as account join account.cards as card where account.id=?


I was trying something like that
Code:
DetachedCriteria accountCriteria = DetachedCriteria.forClass(Account.class);
parentCriteria.add(Expression.idEq(accountId));
DetachedCriteria cardCriteria = accountCriteria.createCriteria("cards");
//apply additional filtering on cardsCriteria
//accountCriteria.setProjection(?)
cards = accountCriteria.getExecutableCriteria(getSession()).list();

but I don't know how set projection to get list of Cards rather than Accounts.

Please note this is unidirectional one-to-many association.

Thanks,
Jack


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

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.