0

i want to select rows from a table in JPA, my query paramter could be null. So if it is null i want to consider all values for that attribute in that table.

Here is my code :

@Query("SELECT art FROM ArtWork art INNER JOIN art.subjects subject "
            + "INNER JOIN art.styles style "
            + "INNER JOIN art.collections collection "
            + "INNER JOIN art.priceBuckets priceBucket "
            + " WHERE ((subject.title) in (:subjectList) "
            + "AND (style.title) in (:styleList)"
            + "AND (collection.title) in (:collectionList)"
            + "AND (priceBucket.title) in (:priceBucketRangeList)"
            + "AND (art.medium is NULL OR art.medium = :medium)"
            + "AND (art.orientation) LIKE:orientation)"
            + ")")

In the code if :medium is null then i want it search on all mediums in the table

Thanks

  • If the medium parameter is null, simply don't include the `AND (art.medium is NULL OR art.medium = :medium)` clause in the query. – JB Nizet Sep 11 '14 at 06:02
  • Thanks JB, but i tried something like this to solve it. AND (:medium is NULL OR art.medium = :medium) – Kousick Shanmugam Nagaraj Sep 11 '14 at 06:24
  • @KaushikShanmugamNagaraj posted answer similar to your problem [here](http://stackoverflow.com/questions/25479414/hibernate-criteria-how-to-create-a-query-for-many-search-fields/25479557#25479557), though it uses `Criteria API`, but concept is same. – Ankur Singhal Sep 11 '14 at 06:25
  • @ ankur-singhal, just try what i have used.. U do not have to check if the paramater is null or not – Kousick Shanmugam Nagaraj Sep 11 '14 at 06:26

0 Answers0