CriteriaBuilder.and & CriteriaBuilder.or, how-to?

后端 未结 1 1367
粉色の甜心
粉色の甜心 2021-02-13 01:53

I\'m trying to change the following HQL to use JPA Criteria:

select distinct d from Department d 
left join fetch d.children c 
where d.parent is null 
and (
            


        
相关标签:
1条回答
  • 2021-02-13 02:49

    Here's the where clause of your JPQL query:

    where d.parent is null 
    and (
        d.name like :term 
        or c.name like :term
        ) 
    

    The where clause contains two predicates:

    d.parent is null 
    

    and

    (d.name like :term 
     or c.name like :term)
    

    The second predicate is an or containing two predicates:

    d.name like :term
    

    and

    c.name like :term
    

    So you need the same structure in your criteria query:

    Predicate orClause = 
        cb.or(cb.like(root.<String>get("name"), param),
              cb.like(children.<String>get("name"), param));
    
    c.where(cb.isNull(root.get("parent")),
            orClause);
    
    0 讨论(0)
提交回复
热议问题