-1
I am using Spring BOOT with Starter JPA and java 8 and I have this query in my Repository :
@Query("select p from Product p where p.name like %:name% and p.costValue = :costValue and p.saleValue= :saleValue")
List<Product> findByExemple(@Param("name") String name, @Param("costValue") Float costValue, @Param("saleValue") Float saleValue);
I need that when the float parameter comes null remove the condition or bring everything, it would be a % in the number field.
Example:
When the costValue parameter comes null bring all. Without the use of filter it would be like doing the Query this way :
@Query("select p from Product p where p.name like %:name% and p.saleValue= :saleValue")
List<Product> findByExemple(@Param("name") String name, @Param("costValue") Float costValue, @Param("saleValue") Float saleValue);
The quickest solution would be not to use
@Query
Instead assemble an HQL or Criteria based on your parole– nullptr
Or even create new ones query methods to consider or not the property, but I do not like this approach much, I prefer my first suggestion for making the code more reusable
– nullptr
I had never used HQL and with your reply I gave a researched and found the best approach. Thank you very much.
– Matheus Felix