0

Suppose I have a Employee class. It has got many fields like id, firstName, lastName, designaton, age, salary and other fields too. Now I am making a Get Query where I want to use all of these fields (required=false) to be passed as Request Params.

But the question is, there could be many combinations like (firstName,age) or (age,salary,lastName) or (designation,age,salary,lastName) and many more like this. So how should I handle all this filters. Shall I have to write each query for each case?

PS: I am using Spring Boot with Spring Data Jpa.

Umang Kamdar
  • 33
  • 2
  • 6

2 Answers2

1

For this you will have to send Object from where you can get your combination. There may be many combination. So from this perspective you will send value as object and for database query you will select your combinations from that object.

If you want different combination, it won't be a good practice to write controller for every combination. So you can send a Object instead of RequestParam value where you can get your combinations from the Object

Example :

Class Employee{
  // Your class instance variable 
  // Which is called your combinations
}

public Employee getEmployeeByName(Employee employee){
  // now you send your desired combination from employee class for 
  // database query
}
naimul
  • 69
  • 1
  • 4
0

Where you have too many fields, it's not a good practice to send all fields as RequestParam. Think of your class getting bigger day by day and you editing the controller method continously.

Better way is send as a object. No need to edit controller later. Only change the entity class

naimul
  • 69
  • 1
  • 4
  • I could not get the whole idea. Could you please brief about this? The idea behind this request params is I want to do filtering. E.g if I want to find the Employee who has specific desgination, lastName, and salary; then I would have to use something like this findByDesignationAndLastNameAndSalary method of Spring Data JPA, but shall I have to use all Combinations considering many such cases are possible in filtering? Can you please give a brief idea how to do it exactly? – Umang Kamdar Sep 30 '21 at 17:58
  • You will write query for a specific combinations. For specific combination you will have to write specific spring data jpa query. As you have written findByDesignationAndLastNameAndSalary for desgination, lastName, and salary Now you can think of findByDesignation for designation findByDesignationAndSalary for designation and salary if you like to use others combination then you will have to write specific query for those fields – naimul Sep 30 '21 at 18:08
  • So yeah, my question was that only. Suppose I have 6 fields that can be chosen while filtering. In such case many combinations are possible (eg like only 1 field in filtering (findByDesignation or findByLastName etc...), 2 fields (findByDesignationAndLastName or findByDesignationAndSalary etc...), 3 fields (findByDesignationAndLastNameAndSalary or findByDesignationAndFirstNameAndSalary etc...), 4 fields and so on...). So how to handle this, since many combinations are possible and its not feasible to write methods for each case. – Umang Kamdar Oct 01 '21 at 13:47
  • Check this: https://stackoverflow.com/a/20283256/11812518 I prefer the QueryDSL approach. – Petar Bivolarski Oct 01 '21 at 13:50
  • no need to add each field for RequestParam, Spring web already takes take of that if the object argument has no annotation. Checkout naimul's answer. – emrhzc May 11 '22 at 07:41