Autonomy
Autonomy

Reputation: 412

Hibernate subquery apply DetachedCriteria to ManyToMany join table

I'm trying to create a subquery that will take a DetachedCriteria that selects from the join table where there are entries for an entity; essentially I want to see if an entity has any associations via the join table.

Depending on a passed boolean I'll add a exists or notExists subquery to the criteria.

Basically I want to take the subquery in this sql:

select * from A as leftEntity where not exists (select * from A_B where A = leftEntity.id);`

and create a detached criteria. I can't figure out the correct entity to pass to create the detached criteria - I'm trying to do something on the join table, not entity A or B. How would I do this?

Upvotes: 1

Views: 764

Answers (1)

Autonomy
Autonomy

Reputation: 412

I gave up trying to use DetachedCriteria and just used an sqlRestriction on the criteria;

criteria.add(Restrictions.sqlRestriction("exists (select a from a_b where a = {alias}.id)"));

Upvotes: 3

Related Questions