spring boot jpa @query where clause based on boolean flag param

Viewed 689

I am using spring boot version 2.4.0.

I need to get the data from table where name is not null but it should conditional based on mandflag, if mandflag=true then only name is not null should be executed. Kindly suggest how to add this flag based condition in below query.

@Query("SELECT p FROM testtable p WHERE name is not null  ")
public List<PatientEntity> findRecords(@Param("mandflag") boolean mandflag);

Edit Requirement- Fetch data from table where name is not null if input flag is true

I need data from table where condition should be executed based on boolean flag

2 Answers
@Query("SELECT p FROM testtable p WHERE CASE WHEN :mandflag= TRUE THEN name is not null ELSE 1=1 END", nativeQuery = true)
public List<PatientEntity> findRecords(@Param("mandflag") boolean mandflag);

Try this:

@Query("SELECT p FROM testtable p WHERE (case when :mandflag = true then name is noy null else name is null end ))
public List<PatientEntity> findRecords(@Param("mandflag") boolean mandflag);
Related