0

I have a model A that has a many to many with B, which has a property c. Like the following.

class A {
  @ManyToMany
  List<B> bs;
}

class B {
  @ManyToMany
  List<A> as;
  String c;
}

Is there a way (using detached criteria) to do a query like this: select all As where there exists a b in bs where b.c = some value? And if I can how would I do this?

Razvi
  • 2,808
  • 5
  • 31
  • 39

2 Answers2

1

I think this should do the trick:

from A a inner join a.bs b where b.c = ?

AndresQ
  • 803
  • 5
  • 19
  • Yeah, it seems to work okay like that. But is there any way to do it with DetachedCriterias? – Razvi Feb 09 '12 at 20:01
1

I research some more and I found a way to do it. For those interested:

DetachedCriteria.forClass(A.class)
  .addAlias("bs", "b")
  .add(Restrictions.eq("b.c", c);
Razvi
  • 2,808
  • 5
  • 31
  • 39