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