views:

80

answers:

3

I am trying to write a HQL Query which selectes rows from a table based on multiple criteria. firstName,lastName

the catch is that the query should be flexible to ignore any empty or null values

so

select t from table t where (:firstname = '' or t.firstName = :firstName) AND
(:lastName = '' OR t.lastName = :lastName)

I would have thought this would work? But it doesnt - it never returns any rows? Any ideas what could be wrong here? I am very new to HQL thats why this question.

A: 

(:firstname = '' or t.firstName = :firstName)

Your criteria is strange. If :firstname = '' and if a firstname (t.firstName) is equal '' in the database, the criteria t.firstName = :firstName is good ('' = '')

You don't need :firstname = ''

But If you want to check null value, you need to do:

t.firstName IS NULL or t.firstName = :firstname
Kiva
This is what I have and it still doesnt work.SELECT p FROM table p WHERE (:firstName is null OR p.firstName LIKE '%:firstName%') AND (:lastName is null OR p.lastName LIKE '%:lastName%') AND (:gender is null OR p.gender= :gender)where firstname and lastname are strings and gender is an integer
Jimmy
what I am trying to do is ignore any null or empty values passed in to the function. which is why I had :firstname = '' initially.
Jimmy
You can't make your like condition like this.You must dofirstname = "%" + firstname + "%";Then, in your query:p.firstName is null OR p.firstName LIKE :firstNamenot do :firstName is null (useless condition, do p.firstName is null)
Kiva
@KivaWhat Jimmy is trying to do is to disable a part of the where clause when his Java method receives a null parameter. I-e if search("Jimmy", null) is called the query should only filter on firstname = "Jimmy" ignoring the lastName column value.
Tahir Akhtar
Yes, Tahir, that is exactly what I am trying to do.
Jimmy
Ok, make your query in several times like this:query = "select t from table t where 1 "if (fisrtname != null)query += " and t.firstName = :firstName"and so and so for all parameters(basic code, you can optimize it)
Kiva
I am trying to do it all in one query if possible. Generating a Dynamic Query is not an Option I want to take. How can we do this in one Query string ?
Jimmy
A: 

What happens if you run following hql with firstname parameter set to empty string?

select t from table t where (:firstname = '') 

and following with firstname parameter set to null:

select t from table t where (:firstname is null) 

If any of the above returns the whole table then the HQLs named parameter might support what you are trying to do.

Otherwise you must use different queries for the null parameter cases. You can do this by generating the query dynamically.

Tahir Akhtar
I am trying to do it all in one query if possible. Generating a Dynamic Query is not an Option I want to take. How can we do this in one Query string ?
Jimmy
Why do you not want to do a dynamic query ?
Kiva
the option is not there. the project is setup that way. long story short - cant do a dynamic query.
Jimmy
A: 

If I am understanding correctly you want a way to allow the user to search by firstName, lastName or both. So you should be checking if the parameter passed in is empty then don't make it a condition. If they supply all blank parameters it would return the whole table. Try:

select t from table t 
where (:firstname IS NULL or t.firstName = :firstName) AND
(:lastName IS NULL OR t.lastName = :lastName)
jschoen
thanks...that was pretty much what I was looking for.
Jimmy