我JpaSpecificationExecutor用于创建自定义查询.如何为以下SQL创建规范?
select * from employee e, address a where e.id=23415 and e.name="Deepak" and a.city="Leeds";
Run Code Online (Sandbox Code Playgroud)
Java类:
public static Specification<Employee> searchEmployee(final Map<String,String> myMap) {
return new Specification<Employee>(){
@Override
public Predicate toPredicate(Root<Employee> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//Need to query two tables Employee and Address
}
}
Run Code Online (Sandbox Code Playgroud) 在使用 JPA 接口时JpaSpecificationExecutor,我创建了以下代码,但它抛出错误:“至少提供了 2 个参数,但查询中仅存在 1 个参数”。
调用代码
@Override
public Result findSightsWithConditions(String cityId, Map<String, String> filter_map) {
//scoreOrder priceOrder minScore maxScore minPrice maxPrice
Specification<Sight> specification = new Specification<Sight>() {
@Override
public Predicate toPredicate(Root<Sight> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
List<Predicate> predicates = Lists.newArrayList();
//predicates.add(criteriaBuilder.equal(root.get("cityId"), cityId));
if (null != filter_map.get("maxScore")) {
double max = Integer.parseInt(filter_map.get("maxScore"));
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("score"), max));
}
if (null != filter_map.get("maxPrice")) {
double max = Integer.parseInt(filter_map.get("maxPrice"));
predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get("price"), max));
}
if (null != filter_map.get("minScore")) {
double min = Double.parseDouble(filter_map.get("minScore")); …Run Code Online (Sandbox Code Playgroud)