1
votes

I would like to do using hibernate criteria and createAlias a query like this:

SELECT
table1."any_field" FROM table1 inner join table2 on table1.field1 = table2.field1 OR table1.field2 = table2.field1

where .....

The main problem is I'm not able to add "OR" condition to the inner join, all restrictions and stuff I apply always becomes "AND".

1
criteria can only walk mapped associations andcan not build arbitrary sql in the from clause. Hql is a better fit for this scenario - Firo

1 Answers

0
votes

certanly not efficient:

session.createCriteria(T2.class, "t2")
    .add(Restriction.or(
        Subquery.exists(DetachedCriteria.for(T1.class).add(Restrictions.eqProperty("field1", "t2.field1")),
        Subquery.exists(DetachedCriteria.for(T1.class).add(Restrictions.eqProperty("field2", "t2.field1")))
    .list();