Filter CriteriaQuery JPA 2.0

时间:2016-07-15 18:44:57

标签: jpa

我正在通过WebLogic服务器10.3.6使用Java 1.6,Jsf 2.0,JPA 2.0,Primefaces 5.3创建应用程序

我需要为某些模块实现延迟数据表的负担,我无法使过滤器工作。

这是加载方法:

public List<T> load(int first, int pageSize, String sortField,
    SortOrder sortOrder, Map<String, Object> filters) {
// TODO Auto-generated method stub
try {
    List<T> list = new ArrayList<T>();

    //Criteria Query
    CriteriaBuilder cb = this.ejbUtils.getEntityManagerPrivate().getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);

    //From
    Root<T> from = cq.from(entityClass);

    //Sort
    if(sortField != null){
        if(sortOrder == SortOrder.ASCENDING){
            cq.orderBy(cb.asc(from.get(sortField)));
        }else{
            cq.orderBy(cb.desc(from.get(sortField)));
        }
    }

    //Filters
    if(!filters.isEmpty()){

        List<Predicate> predicates = new ArrayList<Predicate>();

        for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {

            String filterProperty = it.next();
            Object filterValue = filters.get(filterProperty);                   
            Expression<String> literal = cb.upper(cb.literal((String)filterProperty.toUpperCase()));

            predicates.add(cb.like(literal, String.valueOf("%" + filterValue + "%").toUpperCase()));

        }               
        cq.where(predicates.toArray(new Predicate[predicates.size()]));             
    }   

    Query q = this.ejbUtils.getEntityManagerPrivate().createQuery(cq);

    list = q
            .setFirstResult(first)
            .setMaxResults(pageSize)
            .getResultList();

    this.setRowCount(this.ejbUtils.count());

    return list;
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    return null;
  }
}

我查看日志EclipseLink并将此抛出

[EL Fine]:2016-07-15 11:15:35.415 - ServerSession(1294563329) - 连接(1645363733) - 线程(线程[[ACTIVE] ExecuteThread:'21'表示队列:'weblogic。 kernel.Default(self-tuning)',5,Pooled Threads]) - SELECT * FROM(SELECT / * + FIRST_ROWS / a。,ROWNUM rnum FROM(SELECT ID AS a1,ACTION AS a2,ACTIVA AS a3,FECHA_CREACION AS) a4,FECHA_MODIFICACION as a5,NOMBRE AS a6,USUARIO_CREACION as a7,US_PARIO_MODIFICACION as a8,ID_PADRE as a9 from SE_OPCIONES WHERE UPPER(?)LIKE?)a WHERE ROWNUM&lt; =?)WHERE rnum&gt; ? bind =&gt; [NOMBRE,%ROL%,10,0]

然后当我在数据表中输入过滤器时,永远不会带来结果。

1 个答案:

答案 0 :(得分:1)

我已经修好了,取而代之的是

predicates.add(cb.like(literal, String.valueOf("%" + filterValue + "%").toUpperCase()));

通过

predicates.add(cb.like(cb.upper(from.get(filterProperty).as(String.class)), String.valueOf("%" + filterValue + "%").toUpperCase()));