Given a Predicate
used in a CriteriaQuery
, e.g. this:
Predicate predicate = root.get(MyTable.col1).in("col1Val1", "col1Val2");
Can this be extended to use multiple ANDed fields, e.g. the same as the SQL below?
SELECT *
FROM MyTable
WHERE (col1, col2, col3) IN (
("col1Val1", "col2Val1", "col3Val1"),
("col1Val2", "col2Val2", "col3Val2")
);
You can define an ORDER BY clause with the orderBy method of the CriteriaQuery interface and the asc or desc method of the CriteriaBuilder interface. The following CriteriaQuery returns Book entities in the ascending order of their title attribute. List<Book> books = em. createQuery(cq).
For a particular CriteriaQuery object, the root entity of the query, from which all navigation originates, is called the query root. It is similar to the FROM clause in a JPQL query. Create the query root by calling the from method on the CriteriaQuery instance.
public interface CriteriaBuilder. Used to construct criteria queries, compound selections, expressions, predicates, orderings. Note that Predicate is used instead of Expression<Boolean> in this API in order to work around the fact that Java generics are not compatible with varags. Since: Java Persistence 2.0.
The ORDER BY clause is used to sort the data and arrange them either in ascending or descending order. The CriteriaQuery interface provides orderBy() method to define the type of ordering.
Not so elegant method, using JPA criteria builder
Path<String> col1Path=root.get("col1");
Path<String> col2Path=root.get("col2");
Path<String> col3Path=root.get("col3");
Predicate p0=criteriaBuilder.concat(col1Path,col2Path,col3Path)
.in("col1Val1"||"col2Val1"||"col3Val1",
"col1Val2"|| "col2Val2"|| "col3Val2");
Second Method
Path<String> col1Path=root.get("col1");
Path<String> col2Path=root.get("col2");
Path<String> col3Path=root.get("col3");
Predicate p1=criteriaBuilder.or(
criteriaBuilder.and(criteriaBuilder.equal(col1Path,"col1Val1"),
criteriaBuilder.equal(col2Path,"col2Val1"),
criteriaBuilder.equal(col3Path,"col3Val1")
),
criteriaBuilder.and(criteriaBuilder.equal(col1Path,"col1Val2"),
criteriaBuilder.equal(col2Path,"col2Val2"),
criteriaBuilder.equal(col3Path,"col3Val2")
)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With