简体   繁体   中英

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

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM