Given this schema:
Table 'A':
int A_ID primary key;
varchar name;
Table 'B':
int B_ID primary key;
varchar name;
Table 'A_B':
int A_ID not null foreign key(A);
int B_ID not null foreign key(B);
Then A is tied to B with this annotation defined in A.java:
Code:
@ManyToMany()
@JoinTable (
name = "A_B",
joinColumns = @JoinColumn(name = "A_ID"),
inverseJoinColumns = @JoinColumn(name = "B_ID")
)
/** properties */
private Collection<Property> bees = null;
The reverse tie is done in B as well (although this shouldn't matter). How can I use the Criteria API to generate this query for A objects?
Code:
select A.*
from A as X
inner join A_B as Y on (X.A_ID = Y.A_ID)
inner join B as Z on (Y.B_ID = Z.B_ID)
where Z.name in ('Moo', 'Bark', 'Meow')
I've tried several stabs, the most recent being:
List<B> bees = session.createCriteria(B.class)
.add(Restrictions.in("name", [list of 'Moo', 'Bark', 'Meow'])
.list();
Criteria c = session.createCriteria(A.class)
.add(Restrictions.in("bees", bees));
To no avail.
Also, are there any in-depth books on the Criteria API?