Search code examples
javahibernatejpaeclipselink

How to dynamically generate SQL query based on user’s selections?


This is the same question as:

How to dynamically generate SQL query based on user's selections?

The only difference is, that I'm interested in seeing solutions also using Java/JPA (+possibly EclipseLink or Hibernate specific extensions).

I need to create a GUI, using which users can select several attributes which will be used to query the database to find suitable persons. I'm looking for ideas how to dynamically generate the database query according to user's choices.

Query will contain several fields, but to get the idea I will include only three of those below as an example:

  • Occupation - there can be 0 to n occupation strings. If occupation strings are given, one of them have to match.

  • Age - age can be given as:

    1. exact match (30)
    2. range (e.g. 30-40)
    3. less than a value (-40)
    4. more than a value (30-)

Age parameter is optional in the query. In addition, user can specify whether age is a required parameter. If it's not required, and a person does not have age is his/her profile, age criteria is ignored for this person.

  • Height - similar as age

Example queries:

No criteria has been given:

select * from persons

Only occupation has been given:

select * from persons where occupation = 'dentist'

Several occupations have been given:

select * from persons where (occupation = 'dentist' or occupation = 'engineer')

Age has been given as a greater than value, and it's required to exist on person's profile:

select * from persons where age >= 30

Height has been given as a range, and it's not required to exist on person's profile:

select * from persons where (height is null or (height >= 30 and height <= 40))

Combination of different criteria:

select * from persons where occupation = 'dentist' and age >= 30 and (height is null or (height >= 30 and height <= 40))

I have already implemented code which is capable of generating queries as strings, but it certainly is not too pretty. I'm looking for ideas what would be the most efficient and prettiest way to achieve this.


Solution

  • In Hibernate, you can use Criteria queries.

    In Toplink, we got Expression, and ExpressionBuilder.