How to get data based on 'AND' operator instead of 'LIKE' by using JpaSpecificationExecutorWithProjection's findAll method?

huangapple go评论105阅读模式
英文:

How to get data based on 'AND' operator instead of 'LIKE' by using JpaSpecificationExecutorWithProjection's findAll method?

问题

我是新手对于Hibernate。我的前辈使用了JpaSpecificationExecutorWithProjection(接口)的findAll方法,该方法根据'LIKE'运算符返回结果,但我需要根据'AND'运算符返回结果。请指导我如何解决这个问题。

以下是显示它正在命中基于'LIKE'运算符的查询的代码部分:

  1. where
  2. (
  3. upper(course0_.course_subject) like ?
  4. )
  5. and (
  6. upper(course0_.course_sub_category) like ?
  7. )
  8. and course0_.course_status=?
  9. and (
  10. upper(course0_.course_exam_segment) like ?
  11. )
  12. and (
  13. upper(course0_.course_category) like ?
  14. )
英文:

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

  1. where
  2. (
  3. upper(course0_.course_subject) like ?
  4. )
  5. and (
  6. upper(course0_.course_sub_category) like ?
  7. )
  8. and course0_.course_status=?
  9. and (
  10. upper(course0_.course_exam_segment) like ?
  11. )
  12. and (
  13. upper(course0_.course_category) like ?
  14. )

答案1

得分: 1

这是使用AND运算符的。LIKE用于评估您的各个参数。

如果您不喜欢JPA方法查询,您总是可以在您的存储库中编写自己的查询。以下是一个快速示例,包含了左连接并返回一个Page对象。请在这里查看一些基本的JPQL示例。

  1. @Query("SELECT p FROM Product p "
  2. + "LEFT JOIN p.categories category "
  3. + "WHERE UPPER(p.name) LIKE UPPER(CONCAT('%', COALESCE(:searchRequest, ''), '%')) "
  4. + "AND UPPER(p.description) LIKE UPPER(CONCAT('%', COALESCE(:description, ''), '%')) "
  5. + "AND p.price BETWEEN :priceLow AND :priceHigh "
  6. + "AND p.averageRating >= :averageRating "
  7. + "AND p.archived = :archived "
  8. + "AND ((category.name IN :selectedCategories) "
  9. + "OR (:amountOfSelectedCategories = 0 AND category IN (SELECT c FROM Category c))) "
  10. + "GROUP BY p "
  11. + "HAVING SIZE(p.categories) >= :amountOfSelectedCategories"
  12. )
  13. Page<Product> findAllBySearchModel(
  14. Pageable pageable,
  15. @Param("searchRequest") String searchRequest,
  16. @Param("description") String description,
  17. @Param("priceLow") BigDecimal priceLow,
  18. @Param("priceHigh") BigDecimal priceHigh,
  19. @Param("averageRating") double averageRating,
  20. @Param("archived") boolean archived,
  21. @Param("selectedCategories") List<String> selectedCategories,
  22. @Param("amountOfSelectedCategories") int amountOfSelectedCategories
  23. );
英文:

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

  1. @Query(&quot;SELECT p FROM Product p &quot;
  2. + &quot;LEFT JOIN p.categories category &quot;
  3. + &quot;WHERE UPPER(p.name) LIKE UPPER(CONCAT(&#39;%&#39;, COALESCE(:searchRequest, &#39;&#39;), &#39;%&#39;)) &quot;
  4. + &quot;AND UPPER(p.description) LIKE UPPER(CONCAT(&#39;%&#39;, COALESCE(:description, &#39;&#39;), &#39;%&#39;)) &quot;
  5. + &quot;AND p.price BETWEEN :priceLow AND :priceHigh &quot;
  6. + &quot;AND p.averageRating &gt;= :averageRating &quot;
  7. + &quot;AND p.archived = :archived &quot;
  8. + &quot;AND ((category.name IN :selectedCategories) &quot;
  9. + &quot;OR (:amountOfSelectedCategories = 0 AND category IN (SELECT c FROM Category c))) &quot;
  10. + &quot;GROUP BY p &quot;
  11. + &quot;HAVING SIZE(p.categories) &gt;= :amountOfSelectedCategories&quot;
  12. )
  13. Page&lt;Product&gt; findAllBySearchModel(
  14. Pageable pageable,
  15. @Param(&quot;searchRequest&quot;) String searchRequest,
  16. @Param(&quot;description&quot;) String description,
  17. @Param(&quot;priceLow&quot;) BigDecimal priceLow,
  18. @Param(&quot;priceHigh&quot;) BigDecimal priceHigh,
  19. @Param(&quot;averageRating&quot;) double averageRating,
  20. @Param(&quot;archived&quot;) boolean archived,
  21. @Param(&quot;selectedCategories&quot;) List&lt;String&gt; selectedCategories,
  22. @Param(&quot;amountOfSelectedCategories&quot;) int amountOfSelectedCategories
  23. );

huangapple
  • 本文由 发表于 2020年7月31日 21:52:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63193152.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定