Spring 条件 Api 规范枚举列表匹配列表值
Spring Criteria Api Specification enum list match list values
我有这样的东西
public class Car {
private List<CarCategory> categories;
...
}
所以每辆车都可以有多个枚举类别,例如“FAMILY”、“SPORTCAR”、“PREMIUM”、“AFFORDABLE”等
我需要能够获得 specified/given 列表中所有类别的所有汽车,例如“所有具有 FAMILY 和 AFFORDABLE 的汽车”。
我找到的所有使用“builder.in”的例子都假设一辆汽车只能有一个类别,但这不是我想要的
非常感谢任何帮助,谢谢
从查询的角度来看,它可能不是最优的,但像这样的东西在功能上是正确的:
List<String> categories = new ArrayList<>();
cagetories = getCategoriesToFilter();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Car> cq = cb.createQuery(Car.class);
Root<Car> root = cq.from(Car.class);
for (String category : categories){
//We create an inner join to filter those cars that do not have a category
Join<Car,CarCategory> join = root.join("categories",JoinType.INNER);
join.on(cb.equals(join.get("description"),category));
}
cq.select(root);
//Due to the inner joins, at this point all the cars that have all the categories of
//"categories" will appear.
List<Car> result = entityManager.createQuery(cq).getResultList();
我有这样的东西
public class Car {
private List<CarCategory> categories;
...
}
所以每辆车都可以有多个枚举类别,例如“FAMILY”、“SPORTCAR”、“PREMIUM”、“AFFORDABLE”等
我需要能够获得 specified/given 列表中所有类别的所有汽车,例如“所有具有 FAMILY 和 AFFORDABLE 的汽车”。 我找到的所有使用“builder.in”的例子都假设一辆汽车只能有一个类别,但这不是我想要的
非常感谢任何帮助,谢谢
从查询的角度来看,它可能不是最优的,但像这样的东西在功能上是正确的:
List<String> categories = new ArrayList<>();
cagetories = getCategoriesToFilter();
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Car> cq = cb.createQuery(Car.class);
Root<Car> root = cq.from(Car.class);
for (String category : categories){
//We create an inner join to filter those cars that do not have a category
Join<Car,CarCategory> join = root.join("categories",JoinType.INNER);
join.on(cb.equals(join.get("description"),category));
}
cq.select(root);
//Due to the inner joins, at this point all the cars that have all the categories of
//"categories" will appear.
List<Car> result = entityManager.createQuery(cq).getResultList();