ejb@glassfish.java.net

RE: query returning null data

From: Eve Pokua <gorgeous65_at_msn.com>
Date: Mon, 25 Jan 2010 22:16:07 +0000

Hello everyone,

 

If I try the following I get the following results-

 

 public Customerent custLogin(Customerent customer)throws ShoppingException{

          Customerent customers= new Customerent();
                  
            try{
                 customers=em.find(Customerent.class, customer.getCusId());
                 System.out.println("I have found the customer ID" + customer);
                            Query qt
                             = em.createQuery("select c from Customerent c where c.cusId=?");
                   qt.setParameter(1, customers.getCusId());
                   return (Customerent)qt.getSingleResult();
            }catch (Exception e) {
                            logger.error("There was an error getting the customer login details. exception: " + e);
                            return customer;
                             
           }

            

        }

 

Results-

 

username: 1
password: p
I have found the customer IDentityJB.Customerent[cusId = 1nullnullnullnull0p]
22:06:08,613 ERROR [ShoppingManager] There was an error getting the customer login details. exception: java.lang.IllegalArgumentException: An exception occured while creating a query in EntityManager
customer id: entityJB.Customerent[cusId = 1nullnullnullnull0p]


 

ANd if I try the following -

 

public Customerent custLogin(Customerent customer)throws ShoppingException{

          Customerent customers= new Customerent();
                  
            try{
                 customers=em.find(Customerent.class, customer.getCusId());
                 System.out.println("I have found the customer ID" + customer);
                            Query qt
                             = em.createQuery("select c from Customerent c where c.cusId=?");
                   qt.setParameter(1, customer.getCusId());
                   return (Customerent)qt.getSingleResult();
            }catch (Exception e) {
                            logger.error("There was an error getting the customer login details. exception: " + e);
                            return customer;
                             
           }

            

        }


 

Results-

 

username: 1
password: p
I have found the customer IDentityJB.Customerent[cusId = 1nullnullnullnull0p]
22:08:05,414 ERROR [ShoppingManager] There was an error getting the customer login details. exception: java.lang.IllegalArgumentException: An exception occured while creating a query in EntityManager
customer id: entityJB.Customerent[cusId = 1nullnullnullnull0p]



 

I am using jsf - here is the client -

 

<h:form >
                <h:panelGrid id="panel" columns="2" border="1">
                  <f:facet name="header">
                    <h:outputText value="My Login Page"/>
                  </f:facet>
                  <h:outputLabel for="customerid" value="Customer ID" />
                  <h:inputText id="customerid" value="#{customer.cusId}" />
                  <h:outputLabel for="password" value="password" />
                  <h:inputText id="password" value="#{customer.password}" />
                  <f:facet name="footer">
                    <h:panelGroup style="display:block; text-align:center">
                        <h:commandButton id="submit" value="Login" action="#{pc_Login.login}" />
                        <h:commandButton id="custreg" value="Register" action="#{pc_Items.custRegisteration}" />
                    </h:panelGroup>
                  </f:facet>
                </h:panelGrid>
       </h:form>

 

 

I have an faces_config.xml with the following conifg -

 

<managed-bean>
   <managed-bean-name>pc_Login</managed-bean-name>
                    <managed-bean-class>shoppingcart.view.Login</managed-bean-class>
                    <managed-bean-scope>request</managed-bean-scope>
                <managed-property>
                    <property-name>customer</property-name>
                    <value>#{customer}</value>
                </managed-property>
                </managed-bean>
        <managed-bean>
            <managed-bean-name>customer</managed-bean-name>
            <managed-bean-class>entityJB.Customerent</managed-bean-class>
            <managed-bean-scope>request</managed-bean-scope>
        </managed-bean>

 

 

The Login class have the following -

 

 public String login() {
    
  if(customer!=null){
       System.out.println("username: " + customer.getCusId());
       System.out.println("password: " + customer.getPassword());
      Customerent saved = getShoppingviewhelper().custLogin(customer);
      if (saved!=null) {
     System.out.println("customer id: " + saved.toString());}
      else
          System.out.println("Data returned is a null");}
      return null;

  }

 

   public ShoppingViewHelper getShoppingviewhelper() {
  return (ShoppingViewHelper)ManagedBeanUtil.getFacesManagedBean("shoppingViewHelper", FacesContext.getCurrentInstance());
 }

}

 

Basically, I am trying to get a user to either login or register into my DB. So

the with the above, the user is loging in. So what I'm I doing wrong?

 

It does not seem to be going into the DB that's why the results

is a null -

 

customer id: entityJB.Customerent[cusId = 1nullnullnullnull0p]

 

But it prints the data entered by the user - in bold

 

Thanks

 

eve


 

 


From: gorgeous65_at_msn.com
To: persistence_at_glassfish.dev.java.net
CC: ejb_at_glassfish.dev.java.net
Date: Mon, 25 Jan 2010 14:58:24 +0000
Subject: RE: query returning null data



Chris
 
I've tried either way and it was not working. I am using
Jsf webclients. I will send further information of steps
I am applying so you could understand my problem.
 
Thanks
 
eve
 
> Date: Mon, 25 Jan 2010 09:01:12 -0500
> From: christopher.delahunt_at_oracle.com
> To: persistence_at_glassfish.dev.java.net
> CC: ejb_at_glassfish.dev.java.net
> Subject: Re: FW: query returning null data
>
> Hello Eve,
>
> This example is wrong and should get an NPE on customers.getCusid(). I
> think you mixed up customer and customers in the parameter and the
> return variable.
>
> Best Regards,
> Chris
>
> Eve Pokua wrote:
> >
> >
> > ------------------------------------------------------------------------
> > From: gorgeous65_at_msn.com
> > To: users_at_glassfish.dev.java.net
> > Date: Fri, 22 Jan 2010 00:23:49 +0000
> > Subject: query returning null data
> >
> >
> > Hello everyone,
> >
> > I'm trying to use the following query to return data but I'm
> > sure if I'm on the right track -
> >
> > public Customerent custLogin(Customerent customer)throws
> > ShoppingException{
> > Customerent customers= null;
> >
> > try{
> > Query qt
> > = em.createQuery("select c from
> > Customerent c where c.cusid=?");
> > qt.setParameter(1, customers.getCusid());
> > return (Customerent)qt.getSingleResult();
> > }catch (Exception e) {
> > logger.error("There was an error getting
> > the customer login details. exception: " + e);
> > return customer;
> >
> > }
> >
> > }
> >
> > I'm not getting the results I want.
> >
> > Thanks
> >
> > eve
> >
> > ------------------------------------------------------------------------
> > Do you have a story that started on Hotmail? Tell us now
> > <http://clk.atdmt.com/UKM/go/195013117/direct/01/>
> > ------------------------------------------------------------------------
> > Not got a Hotmail account? Sign-up now - Free
> > <http://clk.atdmt.com/UKM/go/197222280/direct/01/>



Do you have a story that started on Hotmail? Tell us now
_________________________________________________________________
Send us your Hotmail stories and be featured in our newsletter
http://clk.atdmt.com/UKM/go/195013117/direct/01/