cn.patterncat.helper.sql.builder.JpaSpecBuilder Maven / Gradle / Ivy
package cn.patterncat.helper.sql.builder;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
/**
* Created by patterncat on 2018-04-17.
*/
public class JpaSpecBuilder {
private Specifications specs;
private JpaSpecBuilder() {
Specification start = (Root root, CriteriaQuery> query, CriteriaBuilder cb) -> cb.isTrue(cb.literal(true));
this.specs = Specifications.where(start);
}
public static JpaSpecBuilder newInstance(){
return new JpaSpecBuilder<>();
}
public JpaSpecBuilder or(final Specification spec) {
specs = specs.or(spec);
return this;
}
public JpaSpecBuilder and(final Specification spec) {
specs = specs.and(spec);
return this;
}
public Specification build() {
return specs;
}
}