Question

I am trying to create a detached criteria that has an OR restriction

    DetachedCriteria subquery = DetachedCriteria.forClass(Component.class);
    subquery.createCriteria("review").add(Restrictions.eq("owner", user));
    subquery.createCriteria("review").createCriteria("observers").
                                   add(Restrictions.eq("id", user.getId())); 

so bascially I need to put the Restriction.or for the second and third line of the code above, however the Restriction.or does not except DetachedCritera. So I need to do something like this

  Restrictions.or(
        subquery.createCriteria("review").add(Restrictions.eq("owner", user)),
        subquery.createCriteria("review").createCriteria("observers").
                                   add(Restrictions.eq("id", user.getId()));

However this wont compile. So how do I achieve this? Any suggestions?

Thanks in advance. cheers

Was it helpful?

Solution

DetachedCriteria subquery = DetachedCriteria.forClass(Component.class, "component");
subquery.createAlias("component.review", "review");
subquery.createAlias("review.observers", "observer", Criteria.LEFT_JOIN);
subquery.add(Restrictions.or(Restrictions.eq("review.owner", user),
                             Restrictions.eq("observer.id", user.getId())));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top