Search code examples
hibernatehibernate-criteriarestriction

Need to equate more than one property in criteria on joining the tables


I need to implement the following code using criteria.

left join A a ON a.id = o.id
        and a.rules_id = b.id 
        and a.global_id = gd.id
        WHERE  m.org_name = 'PQR'

But when I am adding a criteria like this

 criteria = criteria.createAlias("a", "a", JoinType.LEFT_OUTER_JOIN,
                    Restrictions.eqProperty("a.id", "b.id")).add(Restrictions.eqProperty("a.rules_id", "b.id")).add(Restrictions.eqProperty("a.global_id", "gd.id"));

it is going in the where clause of the query as follows:

  left join A a ON a.id = o.id
        WHERE  m.org_name = 'PQR'  and a.rules_id = b.id 
        and a.global_id = gd.id

Can anyone help me with the correct code what can be done so that the join condition will be correct?

I tried with this:

criteria = criteria.createAlias("a", "a", JoinType.LEFT_OUTER_JOIN,
                    Restrictions.eqProperty("a.id", "b.id")).add(Restrictions.eqProperty("a.rules_id", "b.id")).add(Restrictions.eqProperty("a.global_id", "gd.id"));

Need this kind of query:

left join A a ON a.id = o.id
        and a.rules_id = b.id 
        and a.global_id = gd.id
        where  m.org_name = 'PQR'

Solution

  • We need to use restriction conjunction in this scenario.

    criteria = criteria.createAlias("a", "a", JoinType.LEFT_OUTER_JOIN,
                    Restrictions.conjunction(
                    Restrictions.eqProperty("a.id","o.id"),
                    Restrictions.eqProperty("a.rules_id", "b.id")));