All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wenhao.jpa.specification.BetweenSpecification Maven / Gradle / Ivy

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 BetweenSpecification extends AbstractSpecification {
    private final String property;
    private final Comparable lower;
    private final Comparable upper;

    public BetweenSpecification(String property, Object lower, Object upper) {
        this.property = property;
        this.lower = (Comparable) lower;
        this.upper = (Comparable) upper;
    }

    @Override
    public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
        From from = getRoot(property, root);
        String field = getProperty(property);
        return cb.between(from.get(field), lower, upper);
    }
}