persistence@glassfish.java.net

Re: _at_NamedQuery for finding details from more than 1 column

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Fri, 04 May 2007 10:02:44 -0700

Eve,

Unless in real life you have 100 'OR's in your query, I suggest you predefine
those 3 queries and depending on the parameter in your method, call the specific
one.

Another option would be to predefined description, itmname, and itmid that can
never exist and use them as the other 3 parameters to the query - as you have OR
operator they won't affect the result.

Regards,
-marina

Eve Pokua wrote:
> Wouter,
>
> Thanks for your reply I have corrected it as:
>
> @NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
> Iteamdetailsent i WHERE i.description= :description OR i.itmid=:itmid OR
> i.itmname=:itmname").
>
> And I get not deployment errors
>
> But somehow I am still not quite sure about my method:
>
> //retrieving items by description/ item code or item name
> public List<Iteamdetailhlp> Despcodename(String description, String
> itmname, String itmid){
> List<Iteamdetailsent> iteamdetailsent=null;
> //Iteamdetailsent iteamdetailsents=null;
> if (description!=null){
> try{
>
> iteamdetailsent=(List<Iteamdetailsent>)
> em.createNamedQuery("GetItemsbydescption").setParameter("description",
> description).getResultList();
> //iteamdetailsent=(List<Iteamdetailsent>)
> em.createNamedQuery("Getitemswithname").setParameter("itmname",
> itmname).getResultList();
> }catch(Exception ex){
> System.err.println("Can not get items details by Description
> entered. Name entered not recogniced");
>
> }//end of catch
> }
> else if (itmname!=null){
> iteamdetailsent=(List<Iteamdetailsent>)
> em.createNamedQuery("Getitemswithname").setParameter("itmname",
> itmname).getResultList();
> }
> else if(itmid!=null){
>
> iteamdetailsent=(List<Iteamdetailsent>)em.find(Iteamdetailsent.class,
> itmid);}
>
> return copyToDetails(iteamdetailsent);
> }//end of Despcodename
>
>
> As in the client, it is expecting 3 strings. what I really want to do is
> to supply it with one string
> but that complicates these three queries:
>
> iteamdetailsent=(List<Iteamdetailsent>)
> em.createNamedQuery("GetItemsbydescption").setParameter("description",
> description).getResultList();
>
>
> iteamdetailsent=(List<Iteamdetailsent>)
> em.createNamedQuery("Getitemswithname").setParameter("itmname",
> itmname).getResultList();
>
>
>
> iteamdetailsent=(List<Iteamdetailsent>)em.find(Iteamdetailsent.class,
> itmid);}
>
>
> As you can see above, the three queries are doing three different things.
>
> Any suggestions would be appreciated.
>
> Thank you all.
>
> eve
>
>
>
>
>
>
>
>
>
>> From: Wouter van Reeven <wouter_at_van.reeven.nl>
>> Reply-To: persistence_at_glassfish.dev.java.net
>> To: persistence_at_glassfish.dev.java.net
>> Subject: Re: @NamedQuery for finding details from more than 1 column
>> Date: Fri, 4 May 2007 16:56:38 +0200
>>
>> Hi Eve,
>>
>> On Fri, May 04, 2007 at 03:44:37PM +0100, Eve Pokua wrote:
>> > @NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
>> > Iteamdetailsent i WHERE i.description= :description i.itmid=:itmid Or
>> > i.itmname=:itmname"),
>>
>> Shouldn't that be
>>
>> @NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
>> Iteamdetailsent i WHERE i.description= :description or i.itmid=:itmid Or
>> i.itmname=:itmname")
>>
>> so another OR in between :description and i.itmid?
>>
>>
>> HTH, Wouter van Reeven
>>
>> --
>>
>> People: "If she weighs the same as a Duck, she's made of wood!"
>> Sir Bedevere: "And therefore...?"
>
>
> _________________________________________________________________
> Txt a lot? Get Messenger FREE on your mobile.
> https://livemessenger.mobile.uk.msn.com/