Reputation: 719
Right now I have 3 tables in database:
Table A:
id:
...
Table B:
id:
...
Table C:
A_id:
B_id:
...
The mapped object is like this:
@Entity
@Table(name="A")
Class A{
Long id;
@ManyToMany
@Fetch(FetchMode.SELECT)
@JoinTable(name="C",joinColumns={@JoinColumn(name="A_id")},inverseJoinColumns={@JoinColumn(name="B_id")})
List<B> Bs;
}
@Entity
@Table(name="B")
Class B{
Long id;
}
Now I would like to use DetachedCriteria to do a query to find all A entities with A.Bs contains a specific B. Is it possible to do the query with DetachedCriteria.
I think the sql should be
Select A.*
From A inner join C on A.id=C.A_id inner join B on B.id=C.B_id
Where B.id=?
Thanks a lot! Any help will be greatly appreciated.
Upvotes: 1
Views: 1631
Reputation: 691745
In HQL, it would simply be
select a from A a inner join a.bs b where b.id = :id
And it translates directly to criteria:
Criteria c = session.createCriteria(A.class, "a");
c.createAlias("a.bs", "b");
c.add(Restrictions.eq("b.id", bId);
As always, the link to the documentation explaining it.
Upvotes: 4