简体   繁体   中英

OR clause in LEFT_JOIN in hibernate criteria

How can provide OR clause in LEFT_JOIN in hibernate criteria.

Criteria mainCrit=hibernateSession.createCriteria("Main.class");    
Criteria aPropertyCrit=mainCrit.createCrieria("child",CriteriaSpecification.LEFT_JOIN);

It generates sql as

select this_.Id,this_childId....
from main as this_
left join child as child1_ on child1_ .id=this_.childId
where.....

And I need to generate the sql as

select this_.Id,this_childId....
from main as this_
left join child as child1_ on child1_.id=this_.childId or child_.ParentId=this_.childId
where.....

How do I provide OR clause in LEFT_JOIN in above criteria.

The extending of the JOIN ... ON part, could be done, even with Criteria API. But only to be more restrictive, ie adding the AND part.

Take a look on the overloads of the createCriteria method, mostly the last parameter withClause :

Criteria createCriteria(String associationPath,
                        String alias,
                        JoinType joinType,
                        Criterion withClause)
                        throws HibernateException

Create a new Criteria, "rooted" at the associated entity, assigning the given alias and using the specified join type.

Parameters:

  • associationPath - A dot-seperated property path
  • alias - The alias to assign to the joined association (for later reference).
  • joinType - The type of join to use.
  • withClause - The criteria to be added to the join condition (ON clause)

In fact, any other approach (eg adding the OR) , is breaking the mapping principal, introducing the ability to have the cross join at the end. see Hibernate criteria: Joining table without a mapped association

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM