真正动态的 JPA CriteriaBuilder

2022-08-31 22:26:12

我需要创建一个“真实”的动态JPA。我得到了一个与语句。它看起来像这样:CriteriaBuilderMap<String, String>

name : John
surname : Smith
email : email@email.de

...more pairs possible

这是我实现的:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<User> query = cb.createQuery(User.class);
Root<User> userRoot = query.from(User.class);
query.select(userRoot);

List<Predicate> predicates = new ArrayList<Predicate>();
Iterator<String> column = statements.keySet().iterator();
while (column.hasNext()) {

    // get the pairs
    String colIndex = column.next();
    String colValue = statements.get(colIndex);

    // create the statement
    Predicate pAnd = cb.conjunction();
    pAnd = cb.and(pAnd, cb.equal(userRoot.get(colIndex), colValue));
    predicates.add(pAnd);
}

// doesn't work, i don't know how many predicates i have -> can not address them
query.where(predicates.get(0), predicates.get(1), ...);

// doesn't work, because it is a list of predicates
query.where(predicates);

// doesn't work, because the actual predicate overwrites the old predicate
for (Predicate pre : predicates) {
     query.where(pre)
}

我试图构建一个大的,它包含所有其他谓词,并将其添加到 中,但谓词再次覆盖了旧的值。看起来不可能添加一个而不是改变一个谓词:-(Predicatequery.where()Predicate

真正的项目甚至更复杂,因为有些对需要一个,而另一些则需要一个。这甚至还不够:可以有一个额外的语句,其中包含,例如.在这里,值必须拆分并创建一个语句,如下所示:equallikeortype : 1;4;7

<rest of statement> AND (type = 1 OR type = 4 OR type = 7)

更新和解决方案有两个列表,第一个列表为AND工作得很好。第二个列表包含 OR 语句,如 exspected:

final List<Predicate> andPredicates = new ArrayList<Predicate>();
final List<Predicate> orPredicates = new ArrayList<Predicate>();
for (final Entry<String, String> entry : statements.entrySet()) {
    final String colIndex = entry.getKey();
    final String colValue = entry.getValue();
    if (colIndex != null && colValue != null) {

        if (!colValue.contains(";")) {
            if (equals) {
                andPredicates.add(cb.equal(userRoot.get(colIndex), colValue));
            } else {
                andPredicates.add(cb.like(userRoot.<String> get(colIndex), "%" + colValue + "%"));
            }
        } else {
            String[] values = colValue.split(";");
            for (String value : values) {
                orPredicates.add(cb.or(cb.equal(userRoot.get(colIndex), value)));
            }
        }       
    }
}

// Here goes the magic to combine both lists
if (andPredicates.size() > 0 && orPredicates.size() == 0) {
    // no need to make new predicate, it is already a conjunction
    query.where(andPredicates.toArray(new Predicate[andPredicates.size()]));
} else if (andPredicates.size() == 0 && orPredicates.size() > 0) {
    // make a disjunction, this part is missing above
    Predicate p = cb.disjunction();
    p = cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]));
    query.where(p);
} else {
    // both types of statements combined
    Predicate o = cb.and(andPredicates.toArray(new Predicate[andPredicates.size()]));
    Predicate p = cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]));
    query.where(o, p);
}

query.where(predicates.toArray(new Predicate[predicates.size()]));
users = em.createQuery(query).getResultList();

答案 1

您可以将谓词数组传递给 ,决定或随用随到。为此,请生成一个列表,并将列表的内容打包到单个语句中的数组中。喜欢这个:CriteriaBuilderequallikeand

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

for (final Entry<String, String> e : myPredicateMap.entrySet()) {

    final String key = e.getKey();
    final String value = e.getValue();

    if ((key != null) && (value != null)) {

        if (value.contains("%")) {
            predicates.add(criteriaBuilder.like(root.<String> get(key), value));
        } else {
            predicates.add(criteriaBuilder.equal(root.get(key), value));
        }
    }
}

query.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
query.select(count);

如果需要在 和 之间区分,请使用两个列表。andor


答案 2

一种选择是使用具有可变数量参数的方法可以采用数组的事实:

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

或者,您可以将它们组合成一个谓词(请注意,如果不这样做,则不需要像示例中那样创建连词);:

Predicate where = cb.conjunction();
while (column.hasNext()) {
    ...
    where = cb.and(where, cb.equal(userRoot.get(colIndex), colValue));
}

query.where(where);

推荐