Hibernate Restcrition Criteria OR with DetachedCritera
-
14-07-2021 - |
質問
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
解決
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())));
所属していません StackOverflow