animor
asked on
EJB QL 'LIKE' CLAUSE
Hi. I am currently making a search module using EJB QL for the search Query (Oracle Database). I am having a problem on searching with NULL values. It goes this way:
This is the current EJB QL that we use:
select * from TABLE1 t1,TABLE2 t2 WHERE t1.deal_project_id=t2.proj ect_id
AND t2.title LIKE ?1
AND t2.description LIKE ?2
AND t1.contract LIKE ?3
AND t1.deal_type_id LIKE ?4
and t1.network_id=?5
If the user did not specify any string in the textfield we just supply the %% escape character. For example:
select * from TABLE1 t1,TABLE2 t2 WHERE t1.deal_project_id=t2.proj ect_id
AND t2.title LIKE '%%'
AND t2.description LIKE '%%'
AND t1.contract LIKE '%%'
AND t1.deal_type_id LIKE '%%'
and t1.network_id=1
But the problem is, instead of including the columns with NULL in them (Since '%%" means that the user did not specify a criteria in the front end), it ignores it. I know that the solutiom to this is to check if one or more of the criteria is NULL but this will require a lot of EJB finder methods. Does anyone have a good solution to this problem? Your help will be greatly appreciated. Thanks a lot.
Regards!
This is the current EJB QL that we use:
select * from TABLE1 t1,TABLE2 t2 WHERE t1.deal_project_id=t2.proj
AND t2.title LIKE ?1
AND t2.description LIKE ?2
AND t1.contract LIKE ?3
AND t1.deal_type_id LIKE ?4
and t1.network_id=?5
If the user did not specify any string in the textfield we just supply the %% escape character. For example:
select * from TABLE1 t1,TABLE2 t2 WHERE t1.deal_project_id=t2.proj
AND t2.title LIKE '%%'
AND t2.description LIKE '%%'
AND t1.contract LIKE '%%'
AND t1.deal_type_id LIKE '%%'
and t1.network_id=1
But the problem is, instead of including the columns with NULL in them (Since '%%" means that the user did not specify a criteria in the front end), it ignores it. I know that the solutiom to this is to check if one or more of the criteria is NULL but this will require a lot of EJB finder methods. Does anyone have a good solution to this problem? Your help will be greatly appreciated. Thanks a lot.
Regards!
I prefer using LIKE '%%' for all cases.... even in the first case, I would have used LIKE '%1'. Did you try printing the query and see what it is. Run the same query in Oracle and see the result.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A better way would be to build a query in code.
e.g.
Start with
SELECT * FROM TABLE1 t1, TABLE2, t2 WHERE t1.deal_project_id = t2.project_id
Then, for each of the search criteria the end user can input something into, check to see if it is not blank and then append the string ...
"AND field LIKE value_user_typed"
or
"AND field = value_user_typed"
depending upon what you want.
This way if no criteria entered, no filtering is done.
Richard.
e.g.
Start with
SELECT * FROM TABLE1 t1, TABLE2, t2 WHERE t1.deal_project_id = t2.project_id
Then, for each of the search criteria the end user can input something into, check to see if it is not blank and then append the string ...
"AND field LIKE value_user_typed"
or
"AND field = value_user_typed"
depending upon what you want.
This way if no criteria entered, no filtering is done.
Richard.
ASKER
Thanks for the reply guys. RQuadling's first suggestion is good enough to make it work. Since I will be using EJB and we would like to minimize the number of finder methods, it really helps if we can use only one SQL statement to execute the search.
(awards him the points)
Thank you very much =)
(awards him the points)
Thank you very much =)
Thank you.
And I only stumbled into this area today as an accident, looking for the PHP questions!
And I only stumbled into this area today as an accident, looking for the PHP questions!
ASKER
you're welcome. i just noticed that it wont work if the user provides a criteria and still stumbles upon null values, the query will return even those that have null values. Maybe i should check first the categories supplied as what you suggested in your second post.