英文:
How to get data based on 'AND' operator instead of 'LIKE' by using JpaSpecificationExecutorWithProjection's findAll method?
问题
我是新手对于Hibernate。我的前辈使用了JpaSpecificationExecutorWithProjection(接口)的findAll方法,该方法根据'LIKE'运算符返回结果,但我需要根据'AND'运算符返回结果。请指导我如何解决这个问题。
以下是显示它正在命中基于'LIKE'运算符的查询的代码部分:
where
(
upper(course0_.course_subject) like ?
)
and (
upper(course0_.course_sub_category) like ?
)
and course0_.course_status=?
and (
upper(course0_.course_exam_segment) like ?
)
and (
upper(course0_.course_category) like ?
)
英文:
I am new to Hibernate.My senior used 'findAll' method of JpaSpecificationExecutorWithProjection (interface) which is returning result based on a 'LIKE' operator but I required result based on 'AND' operator. Please guide me how I can solve this?.
Below is the part of code which displays it is hitting query based on 'LIKE' operator
where
(
upper(course0_.course_subject) like ?
)
and (
upper(course0_.course_sub_category) like ?
)
and course0_.course_status=?
and (
upper(course0_.course_exam_segment) like ?
)
and (
upper(course0_.course_category) like ?
)
答案1
得分: 1
这是使用AND
运算符的。LIKE
用于评估您的各个参数。
如果您不喜欢JPA方法查询,您总是可以在您的存储库中编写自己的查询。以下是一个快速示例,包含了左连接并返回一个Page对象。请在这里查看一些基本的JPQL示例。
@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
);
英文:
It is using AND
operator. The LIKE
is for evaluating your individual params.
If you don't like the JPA-methodqueries, you can always write your own in your repository. Below is a quick example incorporating a left join and returning a Page-object. Please take a look here for some basic JPQL
@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
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论