您可以通过创建 2 个单独的“相等”谓词并使用这 2 个来创建一个新的谓词来实现此目的,criteriaBuilder.and(predicate1, predicate2);
您的查询应如下所示:
//Sample values to use in where clause
String columnATest = "Jimmy";
String columnBTest = "18";
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
//This will return a list of MyTable objects.
CriteriaQuery<MyTable> cq = cb.createQuery(MyTable.class);
Root<MyTable> root = cq.from(MyTable.class);
Predicate cond = null;
//If both columns should match the test values
cond = cb.and(cb.equal(root.get(MyTable_.columnA), columnATest), cb.equal(root.get(MyTable_.columnB), columnBTest));
//If only one of the 2 columns should match the test values
cond = cb.or(cb.equal(root.get(MyTable_.columnA), columnATest), cb.equal(root.get(MyTable_.columnB), columnBTest));
CriteriaQuery cq = criteriaQuery.select(root).where(cond);
TypedQuery typedQuery = entityManager.createQuery(cq);
List<MyTable> results = (List<MyTable>) typedQuery.getResultList();