persistence@glassfish.java.net

Re: _at_NamedQuery for finding details from more than 1 column

From: Wouter van Reeven <wouter_at_van.reeven.nl>
Date: Tue, 8 May 2007 11:36:00 +0200

A thought occured to me. It may be possible to rewrite the query to always work
after all. Your query now looks like this:

@NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
Iteamdetailsent i WHERE i.description= :description OR i.itmid=:itmid OR
i.itmname=:itmname")

The problem is that you do not always set all three parameters. If you only
sppecify, e.g., the decription, the query translates to something like this:

SELECT i FROM Iteamdetailsent i WHERE i.description="a description" OR
i.itmid= OR i.itmname="

which is invalid. I am not sure if this is possible, but maybe a query like this
will do the trick:

@NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
Iteamdetailsent i WHERE i.description LIKE CONCAT('%',CONCAT(:description,'%')
OR i.itmid LIKE CONCAT('%",CONCAT(:itmid,'%') OR i.itmname LIKE
CONCAT('%',CONCAT(:itmname,'%'"))

I am sure this works in "normal" SQL but I am not sure if this works in EQL. the
part I am not sure about is if the named parameters are picked up correctly.
Basically this makes sure that if no parameters are entered, a "default" value
of '%%' is used. The statement i.description LIKE '%%' always returns true if
the description column contains any data.

I think this construction is rather ugly though.

On Tue, May 08, 2007 at 09:58:39AM +0100, Eve Pokua wrote:
> In the client side, I intend to only supply one textfield and a button
> which when entry is supplied by the client, will search the database. If
> it does not find item details by itmid, it will search the next column:
> description, if it does not, then the next column: itmname.

So why not use a query like this:

@NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
Iteamdetailsent i WHERE i.description= :item OR i.itmid=:item OR
i.itmname=:item")

This will return a result if any of the three columns matches. Please note that
only EXACT matches will be returned. If you need to have a substring match
please use LIKE instead of = (equals).

> I think, if this don't work, I will separate all of them and use the drop
> down menu.
> So if user enters 'description', it calls the description method, and if
> they enters itemid/code, it calls
> the itemid method, and if by itemname, it calls the itemname method.


HTH, Wouter

-- 
People: "If she weighs the same as a Duck, she's made of wood!"
Sir Bedevere: "And therefore...?"