Java openjpa标准生成器和或

Java openjpa标准生成器和或,java,openjpa,Java,Openjpa,嗨,我需要做一个复杂的查询,使用and或条件。但是and条件似乎覆盖了or条件这是我的代码: public List<UtenteEntity> search(CartesioPojo params) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<UtenteEntity> q = cb.createQuery(UtenteEntity.class);

嗨,我需要做一个复杂的查询,使用and或条件。但是and条件似乎覆盖了or条件这是我的代码:

public List<UtenteEntity> search(CartesioPojo params) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<UtenteEntity> q = cb.createQuery(UtenteEntity.class);
    Root<UtenteEntity> c = q.from(UtenteEntity.class);
    UtenteParams utente = (UtenteParams) params;
    List<Predicate> p = new Vector<Predicate>();
    if(utente.getUsername() != null && !utente.getUsername().equals(""))
        p.add(cb.equal(c.get("username"), cb.literal(utente.getUsername())));
    if(utente.getCognome() != null && !utente.getCognome().equals(""))
        p.add(cb.and(cb.equal(c.get("cognome"), cb.literal(utente.getCognome()))));
    if(utente.getRoles() != null && !utente.getRoles().isEmpty()) {
        for (RuoloEntity ruolo : utente.getRoles()) {
            p.add(cb.or(cb.equal(c.get("ruolo"), cb.literal(ruolo))));
        }
    }
    q.where(p.toArray(new Predicate[p.size()]));
    q.orderBy(cb.asc(c.get(USERNAME_COLUMN)));
    TypedQuery<UtenteEntity> query = entityManager.createQuery(q);
    List<UtenteEntity> result = query.getResultList();
    return result;
}

在谓词列表中,您将在循环中添加包含单个元素的or子句:

 p.add(cb.or(cb.equal(c.get("ruolo"), cb.literal(ruolo))));
您需要创建一个与or连接的谓词列表,并将此长
谓词添加到主列表中:

List<Predicate> disjunction = new ArrayList<Predicate>();
for (RuoloEntity ruolo : utente.getRoles()) {
    disjunction.add(cb.equal(c.get("ruolo"), cb.literal(ruolo)));
}
p.add(cb.or(disjunction.toArray(new Predicate[disjunction.size()])));
List disjunction=new ArrayList();
for(RuoloEntity ruolo:ute.getRoles()){
析取加(cb.equal(c.get(“ruolo”),cb.literal(ruolo));
}
p、 添加(cb.or(disjunction.toArray)(新谓词[disjunction.size()]));
List<Predicate> disjunction = new ArrayList<Predicate>();
for (RuoloEntity ruolo : utente.getRoles()) {
    disjunction.add(cb.equal(c.get("ruolo"), cb.literal(ruolo)));
}
p.add(cb.or(disjunction.toArray(new Predicate[disjunction.size()])));