Using Spring Boot 3.2.4
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/>
</parent>
I was able to successfully adapt the query in the question, both with null and non-null parameters, after identifying and fixing some apparent typos.
Having the entity class defined as follows (getters, setters omitted)
@Table
@Entity
public class TableEntity {
@Id
Long id;
@Column
String employee;
@Column(name = "usr") //user is a reserved SQL word
String user;
@Column
Long salary;
}
and having defined the repository as follows:
@Repository
public interface TestRepository extends JpaRepository<TableEntity, Long> {
@Query(value ="select s from TableEntity s where (coalesce(:users,'1')='1' or s.user in :users) and "
+ "(coalesce(:employees,'1')='1' or s.employee in :employees) and "
+ "(:number is null or s.salary=:number)")
List<TableEntity> findTable(@Param("users") List<String> users,
@Param("employees") List<String> employees,
@Param("number") Long number,
Pageable paging);
}
the following repository invocations execute without errors and retrieve the results expected.
List<TableEntity> entities = testRepository.findTable(
Arrays.asList("a", "b", "c"),
Arrays.asList("e1", "e2", "e3"),
15L,
Pageable.ofSize(100));
List<TableEntity> allEntities = testRepository.findTable(
null,
null,
null,
Pageable.ofSize(100));
Note the following:
table s
has been rewritten as TableEntity s
- user is a reserved word in SQL, so the respective column has been named usr
@Param("number" Long number)
has been written as @Param("number") Long number
coalesce(:users,1)='1'
has been rewritten as coalesce(:users,'1')='1'
coalesce(:employees,1)='1'
has been rewritten as coalesce(:employees,'1')='1'
:number=null
has been rewritten as :number is null
@Param("clients") List<String> clients
instead of@Param("clients" List<String> clients)
– Reprintusers
in the query which is not supplied as the argument, and a parameterclients
supplied in the method which is not used in the query. Can you share the actual code? – Reprint