
com.github.wenhao.jpa.specification.EqualSpecification Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jpa-spec Show documentation
Show all versions of jpa-spec Show documentation
A JAP Query By Specification framework.
The newest version!
package com.github.wenhao.jpa.specification;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.From;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
public class EqualSpecification extends AbstractSpecification {
private final String property;
private final Object[] values;
public EqualSpecification(String property, Object... values) {
this.property = property;
this.values = values;
}
@Override
public Predicate toPredicate(Root root, CriteriaQuery> query, CriteriaBuilder cb) {
From from = getRoot(property, root);
String field = getProperty(property);
if (values == null) {
return cb.isNull(from.get(field));
}
if (values.length == 1) {
return getPredicate(from, cb, values[0], field);
}
Predicate[] predicates = new Predicate[values.length];
for (int i = 0; i < values.length; i++) {
predicates[i] = getPredicate(root, cb, values[i], field);
}
return cb.or(predicates);
}
private Predicate getPredicate(From root, CriteriaBuilder cb, Object value, String field) {
return value == null ? cb.isNull(root.get(field)) : cb.equal(root.get(field), value);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy