JPA Criteria api with CONTAINS function

前端 未结 2 1060
星月不相逢
星月不相逢 2020-12-18 02:14

I\'m trying to crete Criteria API query with CONTAINS function(MS SQL):

select * from com.t_person where contains(last_name,\'xxx\')

CriteriaBuilder          


        
2条回答
  •  伪装坚强ぢ
    2020-12-18 02:45

    You could try using the CriteriaBuilder like function instead of the CONTAINS function:

    //Get criteria builder
    CriteriaBuilder cb = em.getCriteriaBuilder();
    //Create the CriteriaQuery for Person object
    CriteriaQuery query = cb.createQuery(Person.class);
    
    //From clause
    Root personRoot = query.from(Person.class);
    
    //Where clause
    query.where(
        //Like predicate
        cb.like(
            //assuming 'lastName' is the property on the Person Java object that is mapped to the last_name column on the Person table.
            personRoot.get("lastName"),
            //Add a named parameter called likeCondition
            cb.parameter(String.class, "likeCondition")));
    
    TypedQuery tq = em.createQuery(query);
    tq.setParameter("likeCondition", "%Doe%");
    List people = tq.getResultList();
    

    This should result in a query similar to:

    select p from PERSON p where p.last_name like '%Doe%';
    

提交回复
热议问题