JPA Query if value is present will check the value is exist

Viewed 25

I found this in https://www.baeldung.com/spring-data-jpa-null-parameters and its working fine in test project but i want to add if param has a value then check if exist in database and if not exist i will try my best to return to empty list.

And im trying to combine with this exist https://www.baeldung.com/spring-data-exists-query but no luck.

Must be a query.

@Query("SELECT c FROM Customer c WHERE (:name is null or c.name = :name) and (:email is null or c.email = :email)")
List<Customer> findCustomerByNameAndEmail(@Param("name") String name, @Param("email") String email);
1 Answers

You have to return your query as boolean

 @query(“Select count(*) > 0 from YOUR_TABLE WHERE …”)
 boolean existsByNameAndEmail(@Param(“name”) String name, @Param(“email”)String email);
Related