Compile error when using CriteriaBuilder

后端 未结 3 1893
-上瘾入骨i
-上瘾入骨i 2021-01-19 10:13

I am trying convert this JPA QL to criteria builder. JBoss 6.0.

\"SELECT ba FROM BankAccount ba WHERE ba.balance >= :amt ORDER BY ba.ownerName ASC\"
         


        
3条回答
  •  终归单人心
    2021-01-19 10:45

    Well, I finally found the right way to call the gt() method. Here is the complete solution. Fully tested in JBoss 6.

    public List findWithBalance(int amount) {
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery cq = cb.createQuery(BankAccount.class);
        Root from = cq.from(BankAccount.class);
    
        ParameterExpression balance = cb.parameter(Integer.class);
        cq.select(from);
    
        //Here is the trick!
        Predicate predicate = cb.gt(from. get("balance"), balance);
    
        cq.where(predicate);
        cq.orderBy(cb.asc(from.get("ownerName")));
    
        TypedQuery query = em.createQuery(cq);
    
        query.setParameter(balance, amount);
    
        return query.getResultList();
    }
    

提交回复
热议问题