本文介绍了JPA查询以选择"IN"中的哪些多个值.条款的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想为以下SQL语句创建一个JPA参数化查询
I want to create a JPA parameterised query for following SQL statement
select * from car where (colour, speed) in (('red', 50), ('blue', 70))
此查询返回预期结果
entityManager.createQuery("from Car c where (c.colour, c.speed) in (('red', 50), ('blue', 70))", Car.class).getResultList();
如何将当前的硬编码值作为参数传递?
How can the currently hardcoded values be passed as parameter?
edit: removed as it doesn't work as expected, see also @Gas comment
推荐答案
在搜索时,我发现了一个非常相似的问题也许这更符合这个问题.
While searching, I found a very similar question here. Perhaps that's more in line with this problem.
但是,我实现了一些稍微不同的东西.
However, I implemented something slightly different.
@Query("SELECT p FROM Product p "
+ "LEFT JOIN p.categories category "
+ "WHERE UPPER(p.name) LIKE UPPER(CONCAT('%', COALESCE(:searchRequest, ''), '%')) "
+ "AND UPPER(p.description) LIKE UPPER(CONCAT('%', COALESCE(:description, ''), '%')) "
+ "AND p.price BETWEEN :priceLow AND :priceHigh "
+ "AND p.averageRating >= :averageRating "
+ "AND p.archived = :archived "
+ "AND ((category.name IN :selectedCategories) "
+ "OR (:amountOfSelectedCategories = 0 AND category IN (SELECT c FROM Category c))) "
+ "GROUP BY p "
+ "HAVING SIZE(p.categories) >= :amountOfSelectedCategories"
)
Page<Product> findAllBySearchModel(
Pageable pageable,
@Param("searchRequest") String searchRequest,
@Param("description") String description,
@Param("priceLow") BigDecimal priceLow,
@Param("priceHigh") BigDecimal priceHigh,
@Param("averageRating") double averageRating,
@Param("archived") boolean archived,
@Param("selectedCategories") List<String> selectedCategories,
@Param("amountOfSelectedCategories") int amountOfSelectedCategories
);
这篇关于JPA查询以选择"IN"中的哪些多个值.条款的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!