persistence@glassfish.java.net

Re: _at_NamedQuery for finding details from more than 1 column

From: Eve Pokua <gorgeous65_at_msn.com>
Date: Thu, 10 May 2007 10:06:40 +0100

Thanks Marina,

I will add more if statements to capture errors and non return values. i.e,
if nothing is return, it display a message to the user to 'try again'.
Something like that. Thanks

eve


>From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
>Reply-To: persistence_at_glassfish.dev.java.net
>To: persistence_at_glassfish.dev.java.net
>CC: ejb_at_glassfish.dev.java.net
>Subject: Re: @NamedQuery for finding details from more than 1 column
>Date: Wed, 09 May 2007 19:14:33 -0700
>
>Eve,
>
>I'm glad it worked out. One more (last?) comment: getResultList() will
>return an empty collection, not an exception is nothing has been found.
>
>regards,
>-marina
>
>Eve Pokua wrote:
>>Hello everyone,
>>
>>Wouter/ marina/ Michael,
>>
>>Your suggestions really works. And if anyone is interested, below is the
>>updated version:
>>
>>In my entity class, i have the following query:
>>
>>@NamedQuery(name = "GetItemsbydescodename",query = "SELECT i FROM
>>Iteamdetailsent i WHERE i.description =:param OR i.itmid =:param OR
>>i.itmname =:param"),
>>
>>
>>......
>>
>>In my implementation class, I have the following method which uses the
>>above query:
>>
>>//retrieving items by description/ item code or item name
>> public List<Iteamdetailhlp> Despcodename(String param){
>> List<Iteamdetailsent> iteamdetailsent=null;
>> //Iteamdetailsent iteamdetailsents=null;
>> if (param!=null){
>> try{
>>
>> iteamdetailsent=(List<Iteamdetailsent>)
>>em.createNamedQuery("GetItemsbydescodename").setParameter("param",
>>param).getResultList();
>> //iteamdetailsent=(List<Iteamdetailsent>)
>>em.createNamedQuery("Getitemswithname").setParameter("itmname",
>>itmname).getResultList();
>> }catch(Exception ex){
>> System.err.println("Can not get items details by
>>Description/code or name entered. Name entered not recogniced");}}
>>
>>
>> return copyToDetails(iteamdetailsent);
>> }//end of Despcodename
>>
>>
>>IN the client, I have a textfield which accepts data from user and call
>>upon the above method:
>>
>>//search by item code or description/name
>> out.println("<form method='get'
>>action='AllItemsser'>");
>> out.println("Find item by Description/Code or
>>Name: <input type=\"text\" name=\"finddescodetxt\">");
>> out.println("<input type='Submit' value='Find'
>>name='find'/>");
>>
>> out.println("</form>");
>>
>>As you can see above there is not more select options.
>>
>>String finddescode=request.getParameter("finddescodetxt");
>> if (finddescode!= null){
>>
>> try{
>>
>> InitialContext ctx=new InitialContext();
>> Iteamdetails
>>iteamdetails=(Iteamdetails)ctx.lookup("IteamdetailsBeanJNDI");
>>
>>iteamdetailhlp=iteamdetails.Despcodename(finddescode);
>>
>> out.println("<TABLE BORDER='1' CELLPADDING='3'
>>CELLSPACING='1'>");
>> out.println("<TR>");
>> out.println("<td>"
>>+"Itemcode");out.println("<td>" +"Name"); out.println("<td>"
>>+"Description"); out.println("<td>" +"Size"); out.println("<td>"
>>+"Colour");out.println("<td>" +"Price");out.println("</td>");
>> out.println("</TR>");
>>
>> for(Iteamdetailhlp itemlist:
>>iteamdetailhlp){
>>
>> out.println("<TR>");
>>
>>
>>out.println("<td>");out.println(itemlist.getitmid());out.println("</td>");
>>out.println("<td>");out.println(itemlist.getitmname());out.println("</td>");
>>
>>out.println("<td>");out.println(itemlist.getdescription());out.println("</td>");
>>
>>out.println("<td>");out.println(itemlist.getitemsize());out.println("</td>");
>>
>>out.println("<td>");out.println(itemlist.getcolour());out.println("</td>");
>>
>>out.println("<td>");out.println(itemlist.getprice());out.println("</td>");
>> out.println("</td>");}
>>
>> out.println("</TR>");
>> out.println("</TABLE>");
>> }catch(Exception ex){
>>
>> System.err.println("Can not access DB");}
>>
>>
>>
>>I really apprecaite all your help.
>>
>>Thanks
>>
>>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: Wed, 9 May 2007 11:40:04 +0200
>>>
>>>Hi Eve,
>>>
>>>
>>>The choice of the user what they want to search for will be a lot easier
>>>if you
>>>code the options for your select box like this
>>>
>>>out.println("<option value="code">by itemcode</option>");
>>>
>>>Now you only need to check for which option was selected to know what the
>>>user
>>>would like to search for.
>>>
>>>
>>>HTH, Wouter
>>>
>>>On Wed, May 09, 2007 at 10:06:44AM +0100, Eve Pokua wrote:
>>> >
>>> > Hello everyone,
>>> >
>>> > Wouter,
>>> > marina,
>>> > Michael,
>>> >
>>> > I have read all your comments and I have willing to try it on, though
>>>I
>>> > nearly gave up all the idea.
>>> > This is what I have done so far but I would like to change it to make
>>>the
>>> > client/User experience a lot
>>> > more simpler:
>>> >
>>> > In my Iteamdetailsent entity I have the following queries:
>>> >
>>> > @NamedQuery(name = "GetItemsbydescption",query = "SELECT i FROM
>>> > Iteamdetailsent i WHERE i.description= :description"),
>>> >
>>> > @NamedQuery(name = "Getitemswithname",query = "SELECT i FROM
>>> > Iteamdetailsent i WHERE i.itmname= :itmname"),
>>> >
>>> >
>>> > I my implementation I have the following methods:
>>> >
>>> > //retrieving items by name
>>> > public List<Iteamdetailhlp> Itemname(String itmname){
>>> > //public String AllItemname(String itemnames){
>>> > List<Iteamdetailsent> iteamdetailsent=null;
>>> >
>>> > try{
>>> >
>>> > iteamdetailsent=(List<Iteamdetailsent>)
>>> > em.createNamedQuery("Getitemswithname").setParameter("itmname",
>>> > itmname).getResultList();
>>> > //.setParameter("yes",
>>>"Discount").getResultList();
>>> >
>>> > }catch(Exception ex){
>>> > System.err.println("Can not get items details by
>>> > name. Name entered not recogniced");
>>> >
>>> > }//end of catch
>>> > return copyToDetails(iteamdetailsent);
>>> > }//end of Itemname(String itmname)
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > public Iteamdetailsent Itemsbyid(String itmid){
>>> > Iteamdetailsent iteamdetailsent=new Iteamdetailsent();
>>> > try{
>>> >
>>> > iteamdetailsent=em.find(Iteamdetailsent.class,
>>> > itmid);
>>> >
>>> >
>>> > }catch(Exception ex){
>>> > System.err.println("Can not access DB");
>>> >
>>> > }//end of catch
>>> > return iteamdetailsent;
>>> > }//end of Itemsbyid
>>> >
>>> >
>>> >
>>> > public List<Iteamdetailhlp> DescriptItem(String description){
>>> > List<Iteamdetailsent> iteamdetailsent=null;
>>> >
>>> > try{
>>> >
>>> > iteamdetailsent=(List<Iteamdetailsent>)
>>> > em.createNamedQuery("GetItemsbydescption").setParameter("description",
>>> > description).getResultList();
>>> >
>>> > }catch(Exception ex){
>>> > System.err.println("Can not get items details by
>>> > Description entered. Name entered not recogniced");
>>> >
>>> > }//end of catch
>>> > return copyToDetails(iteamdetailsent);
>>> > }//end of DescriptItem
>>> >
>>> >
>>> > The above will separate the parameters.
>>> >
>>> > In the client I have the following:
>>> >
>>> > out.println("<form method='get' action='AllItemsser'>");
>>> > out.println("Find item by description/Code:
>>><input
>>> > type=\"text\" name=\"finddescodetxt\">");
>>> > out.println("<select name='finddescodename'>");
>>> > out.println("<option>by itemcode</option>");
>>> > out.println("<option>by item
>>>description</option>");
>>> > out.println("<option>by item name</option>");
>>> > out.println("</select>");
>>> > out.println("<input type='Submit' value='Find'
>>> > name='find'/>");
>>> >
>>> > out.println("</form>");
>>> >
>>> >
>>> > String finddescode=request.getParameter("finddescodetxt");
>>> > if (finddescode!= null && findbydescodename.equals("by
>>> > itemcode")){
>>> > try{
>>> >
>>> > InitialContext ctx=new InitialContext();
>>> >
>>> > Iteamdetails
>>> > iteamdetails=(Iteamdetails)ctx.lookup("IteamdetailsBeanJNDI");
>>> > Iteamdetailsent
>>> > iteamdetailsent=iteamdetails.Itemsbyid(finddescode);
>>> >
>>> > //iteamdetailhlp=iteamdetails.DescriptItem("table");
>>> >
>>> > out.println("<TABLE BORDER='1' CELLPADDING='3'
>>> > CELLSPACING='1'>");
>>> > out.println("<TR>");
>>> > out.println("<td>"
>>> > +"Itemcode");out.println("<td>" +"Name"); out.println("<td>"
>>> > +"Description"); out.println("<td>" +"Size"); out.println("<td>"
>>> > +"Colour");out.println("<td>" +"Price");out.println("</td>");
>>> > out.println("</TR>");
>>> >
>>> >
>>> >
>>> > out.println("<TR>");
>>> >
>>> >
>>> >
>>>out.println("<td>");out.println(iteamdetailsent.getitmid());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(iteamdetailsent.getitmname());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(iteamdetailsent.getdescription());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(iteamdetailsent.getitemsize());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(iteamdetailsent.getcolour());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(iteamdetailsent.getprice());out.println("</td>");
>>>
>>> > out.println("</td>");
>>> >
>>> >
>>> > out.println("</TR>");
>>> > out.println("</TABLE>");
>>> > }catch(Exception ex){
>>> >
>>> > System.err.println("Can not access DB");
>>> > }}
>>> > else if (finddescode!= null && findbydescodename.equals("by
>>>item
>>> > description")){
>>> > try{
>>> >
>>> > InitialContext ctx=new InitialContext();
>>> > Iteamdetails
>>> > iteamdetails=(Iteamdetails)ctx.lookup("IteamdetailsBeanJNDI");
>>> >
>>> > iteamdetailhlp=iteamdetails.DescriptItem(finddescode);
>>> >
>>> > out.println("<TABLE BORDER='1' CELLPADDING='3'
>>> > CELLSPACING='1'>");
>>> > out.println("<TR>");
>>> > out.println("<td>"
>>> > +"Itemcode");out.println("<td>" +"Name"); out.println("<td>"
>>> > +"Description"); out.println("<td>" +"Size"); out.println("<td>"
>>> > +"Colour");out.println("<td>" +"Price");out.println("</td>");
>>> > out.println("</TR>");
>>> >
>>> > for(Iteamdetailhlp itemlist:
>>> > iteamdetailhlp){
>>> >
>>> > out.println("<TR>");
>>> >
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getitmid());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getitmname());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getdescription());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getitemsize());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getcolour());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getprice());out.println("</td>");
>>>
>>> > out.println("</td>");}
>>> >
>>> > out.println("</TR>");
>>> > out.println("</TABLE>");
>>> > }catch(Exception ex){
>>> >
>>> > System.err.println("Can not access DB");
>>> > }}
>>> >
>>> > else if (finddescode!= null && findbydescodename.equals("by
>>>item
>>> > name")){
>>> > try{
>>> >
>>> > InitialContext ctx=new InitialContext();
>>> > Iteamdetails
>>> > iteamdetails=(Iteamdetails)ctx.lookup("IteamdetailsBeanJNDI");
>>> >
>>> > iteamdetailhlp=iteamdetails.Itemname(finddescode);
>>> >
>>> > out.println("<TABLE BORDER='1' CELLPADDING='3'
>>> > CELLSPACING='1'>");
>>> > out.println("<TR>");
>>> > out.println("<td>"
>>> > +"Itemcode");out.println("<td>" +"Name"); out.println("<td>"
>>> > +"Description"); out.println("<td>" +"Size"); out.println("<td>"
>>> > +"Colour");out.println("<td>" +"Price");out.println("</td>");
>>> > out.println("</TR>");
>>> >
>>> > for(Iteamdetailhlp itemlist:
>>> > iteamdetailhlp){
>>> >
>>> > out.println("<TR>");
>>> >
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getitmid());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getitmname());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getdescription());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getitemsize());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getcolour());out.println("</td>");
>>>
>>> >
>>> >
>>>out.println("<td>");out.println(itemlist.getprice());out.println("</td>");
>>>
>>> > out.println("</td>");}
>>> >
>>> > out.println("</TR>");
>>> > out.println("</TABLE>");
>>> > }catch(Exception ex){
>>> >
>>> > System.err.println("Can not access DB");
>>> > }}
>>> >
>>> >
>>> > So to cut things short, I wanted to call upon one implementation
>>>method
>>> > which uses one query like you have suggested. I will make some
>>>changes and
>>> > let you all know the outcome.
>>> >
>>> > Thanks for all you help.
>>> >
>>> > 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: Tue, 8 May 2007 23:00:54 +0200
>>> > >
>>> > >On Tue, May 08, 2007 at 09:42:08PM +0200, Michael Bouschen wrote:
>>> > >> I think your query works. The actual value of the input parameter
>>>might
>>> > >> include the wild card, e.g. q.setParameter("param", "hello%").
>>> > >
>>> > >Hmmm interesting thought. My experience so far with e.g. Oracle's ADF
>>> > >framework
>>> > >has led me to believe that the % in the query will be taken literally
>>> > >since it's
>>> > >part of the parameter and not concatted to the parameter. For some
>>>reason
>>> > >this
>>> > >is different in ADF. I'll have a look at this to see what happens.
>>> > >
>>> > >
>>> > >Thanks, Wouter
>>> > >
>>> > >--
>>> > >
>>> > >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/
>>>--
>>>
>>>People: "If she weighs the same as a Duck, she's made of wood!"
>>>Sir Bedevere: "And therefore...?"
>>
>>
>>_________________________________________________________________
>>Reserve your place in history - Email Britain!
>>http://www.emailbritain.co.uk/

_________________________________________________________________
The next generation of Hotmail is here! http://www.newhotmail.co.uk