自定义 Spring JPA 查询以获取地图
Custom Spring JPA query to fetch a Map
我有一个 Map<String,string>
为 specifications
的实体:
class Product {
Map<String, String> specifications;
}
我必须创建一个带有 2 个参数(键、值)的自定义查询,该查询将获取所有规格与键等于某个值的产品。
例如获取红色商品(伪代码):
loop through products;
fetch product;
join specifications;
get value by key 'color' and compare it to given value 'red'; add to output if true;
next product;
我试过这个查询:
@Query("SELECT p FROM Products p LEFT JOIN FETCH p.specifications, p.reviews WHERE p.specifications.?1 = ?2")
但很明显,它不起作用。
一个可行的解决方案如下:
SELECT p FROM Product p LEFT JOIN p.specifications, p.reviews m
where ( KEY(m) = :1 and :2 in (VALUE(m)) )
我有一个 Map<String,string>
为 specifications
的实体:
class Product {
Map<String, String> specifications;
}
我必须创建一个带有 2 个参数(键、值)的自定义查询,该查询将获取所有规格与键等于某个值的产品。
例如获取红色商品(伪代码):
loop through products;
fetch product;
join specifications;
get value by key 'color' and compare it to given value 'red'; add to output if true; next product;
我试过这个查询:
@Query("SELECT p FROM Products p LEFT JOIN FETCH p.specifications, p.reviews WHERE p.specifications.?1 = ?2")
但很明显,它不起作用。
一个可行的解决方案如下:
SELECT p FROM Product p LEFT JOIN p.specifications, p.reviews m
where ( KEY(m) = :1 and :2 in (VALUE(m)) )