This is the same question as:
How to dynamically generate SQL query based on user selection?
The only difference is that I am also interested in solutions that also use Java / JPA (possibly EclipseLink or Hibernate extensions).
I need to create a graphical interface with which users can select several attributes that will be used to query the database to find suitable people. I am looking for ideas on how to dynamically generate a database query according to user choices.
Query will contain several fields, but to get this idea, I will give only three of the following:
An age parameter is optional in the request. In addition, the user can indicate whether age is a desired parameter. If this is not required, and the person has no age, this is his profile, age criteria are ignored for this person.
Request example:
No criteria were given:
select * from persons
Given only lesson:
select * from persons where occupation = 'dentist'
Several classes were provided:
select * from persons where (occupation = 'dentist' or occupation = 'engineer')
Age was set as greater than the value, and it must exist in the personal profile:
select * from persons where age >= 30
, :
select * from persons where (height is null or (height >= 30 and height <= 40))
:
select * from persons where occupation = 'dentist' and age >= 30 and (height is null or (height >= 30 and height <= 40))
, , , , . , .