How to order result of hibernate based on a specific order

前端 未结 6 1917
别那么骄傲
别那么骄傲 2021-01-07 19:15

I need to send a query to retrieve values that has a specific group of characters as following:

Lets say I am interested in \'XX\' so it should search for any field

6条回答
  •  别那么骄傲
    2021-01-07 20:08

    You can use Predicates in criteria... something like this:

    public List findByParameter(String key, String value, String orderKey)
    
                CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
                CriteriaQuery criteria = builder.createQuery(this.getClazz());
                Root root = criteria.from(Name.getClass());
                criteria.select(root);
                List predicates = new ArrayList();
                predicates.add(builder.equal(root.get(key), value));
                criteria.where(predicates.toArray(new Predicate[predicates.size()]));
                if (orderKey!=null  && !orderKey.isEmpty()) {
                    criteria.orderBy(builder.asc(root.get(orderKey)));
                }
                result = this.entityManager.createQuery(criteria).getResultList();
    
            return result;
    }
    

提交回复
热议问题