JPA Criteria api with CONTAINS function

前端 未结 2 1068
星月不相逢
星月不相逢 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:54

    If you want to stick with using CONTAINS, it should be something like this:

    //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(
        cb.function(
            "CONTAINS", Boolean.class, 
            //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 containsCondition
            cb.parameter(String.class, "containsCondition")));
    
    TypedQuery tq = em.createQuery(query);
    tq.setParameter("containsCondition", "%näh%");
    List people = tq.getResultList();
    

    It seems like some of your code is missing from your question so I'm making a few assumptions in this snippet.

提交回复
热议问题