« Return to Thread: JPA Find Method gives null value

Re: JPA Find Method gives null value

by John Yeary :: Rate this Message:

Reply to Author | View in Thread

omos jigga -- I sent you a working example EJB project from NB 6.7.1 to your email address. This should help you address some of your questions. Make sure that you query for known values like 100 and Jumbo from the web service tester.

On Mon, Oct 19, 2009 at 10:00 PM, John Yeary <johnyeary@...> wrote:
I would have changed the interface to use the Integer instead of Object.

You can construct a JPA query to use LIKE

@WebMethod
    public List<Customer> findByName(@WebParam(name = "customerName") String customerName) {
        Query q = em.createQuery("SELECT c FROM Customer c WHERE c.name LIKE :name");
        q.setParameter("name", customerName);
        return q.getResultList();

    }

On Mon, Oct 19, 2009 at 8:12 PM, omos jigga <omoz4real@...> wrote:
The Implementation code was not written directly by me. It was generated by netbeans from an existing JPA entity which was generated from the database. I generated the DAO by going through persistence > Session Beans for Entity Classes and the implementation session beans was generated for the find method along with methods to create, update and delete. Also
I understand that I could achieve/implement a search query by using some of the generated named queries i.e finding a user in the entity/database by a criteria i.e the findByLastName. Is there a way I can implement this in the Visual webJSF application which has an input text field and a button so that when a user types in a query in the input field and clicks on the button to perform the search action - it returns the value based on the search criteria.

Thanks.
--- On Tue, 10/20/09, Melongo Annabel <melongo_annabel@...> wrote:

> From: Melongo Annabel <melongo_annabel@...>
> Subject: Re: [nbj2ee] JPA Find Method gives null value
> To: nbj2ee@...
> Date: Tuesday, October 20, 2009, 1:26 AM
> In the example that was
> returning null, you try to use an Integer, right? Then I
> don't understand why your implementation looks like
> this:
> public Customer find(Object id) {
>         return em.find(Customer.class, id);
>     }
> If guess right, the Integer was the primarey key, right?
> where is that primary key in your code?
>
> From:
> omos jigga <omoz4real@...>
> To:
>  nbj2ee@...
> Sent: Mon,
> October 19, 2009 5:15:48 PM
> Subject: Re:
> [nbj2ee] JPA Find Method gives null value
>
>
> Hi,
>
> I have got same methods in my entity class. this is the
> full code in my entity class below
>
> * @author omoz4real
>  */
> @Entity
> @Table(name = "CUSTOMER")
> @NamedQueries({@NamedQuery(name =
> "Customer.findAll", query = "SELECT c FROM
> Customer c"), @NamedQuery(name =
> "Customer.findByCustomerId", query = "SELECT
> c FROM Customer c WHERE c.customerId = :customerId"),
> @NamedQuery(name = "Customer.findByFirstName",
> query = "SELECT c FROM Customer c WHERE c.firstName =
> :firstName"), @NamedQuery(name =
> "Customer.findByMiddleName", query = "SELECT
> c FROM Customer c WHERE c.middleName = :middleName"),
> @NamedQuery(name = "Customer.findByLastName",
> query = "SELECT c FROM Customer c WHERE c.lastName =
> :lastName"), @NamedQuery(name =
> "Customer.findByEmail", query = "SELECT c
> FROM Customer c WHERE c.email = :email")})
> public class Customer implements Serializable {
>     private static final long serialVersionUID =
> 1L;
>  
>    @Id
>     @Basic(optional = false)
>     @Column(name = "CUSTOMER_ID")
>     @GeneratedValue
>     private Integer customerId;
>     @Column(name = "FIRST_NAME")
>     private String firstName;
>     @Column(name = "MIDDLE_NAME")
>     private String middleName;
>     @Column(name = "LAST_NAME")
>     private String lastName;
>     @Column(name = "EMAIL")
>     private String email;
>     @OneToMany(mappedBy =
> "customerId")
>     private Collection<CustomerOrder>
> customerOrderCollection;
>     @OneToMany(mappedBy =
> "customerId")
>     private Collection<Address>
> addressCollection;
>     @OneToMany(mappedBy =
> "customerId")
>     private Collection<Telephone>
> telephoneCollection;
>
>     public Customer() {
>    
>  }
>
>     public Customer(Integer customerId) {
>         this.customerId = customerId;
>     }
>
>     public Integer getCustomerId() {
>         return customerId;
>     }
>
>     public void setCustomerId(Integer customerId)
> {
>         this.customerId = customerId;
>     }
>
>     public String getFirstName() {
>         return firstName;
>     }
>
>     public void setFirstName(String firstName) {
>         this.firstName = firstName;
>     }
>
>     public String getMiddleName() {
>         return middleName;
>     }
>
>     public void setMiddleName(String middleName)
> {
>         this.middleName = middleName;
>     }
>
>    
>  public String getLastName() {
>         return lastName;
>     }
>
>     public void setLastName(String lastName) {
>         this.lastName = lastName;
>     }
>
>     public String getEmail() {
>         return email;
>     }
>
>     public void setEmail(String email) {
>         this.email = email;
>     }
>
>     public Collection<CustomerOrder>
> getCustomerOrderCollection() {
>         return
> customerOrderCollection;
>     }
>
>     public void
> setCustomerOrderCollection(Collection<CustomerOrder>
> customerOrderCollection) {
>         this.customerOrderCollection =
> customerOrderCollection;
>     }
>
>     public Collection<Address>
> getAddressCollection() {
>  
>        return addressCollection;
>     }
>
>     public void
> setAddressCollection(Collection<Address>
> addressCollection) {
>         this.addressCollection =
> addressCollection;
>     }
>
>     public Collection<Telephone>
> getTelephoneCollection() {
>         return telephoneCollection;
>     }
>
>     public void
> setTelephoneCollection(Collection<Telephone>
> telephoneCollection) {
>         this.telephoneCollection =
> telephoneCollection;
>     }
>
>     @Override
>     public int hashCode() {
>         int hash = 0;
>         hash += (customerId != null ?
> customerId.hashCode() : 0);
>         return hash;
>     }
>
>     @Override
>     public boolean
>  equals(Object object) {
>         // TODO: Warning - this method
> won't work in the case the id fields are not set
>         if (!(object instanceof
> Customer)) {
>             return false;
>         }
>         Customer other = (Customer)
> object;
>         if ((this.customerId == null
> && other.customerId != null) || (this.customerId !=
> null && !this.customerId.equals(other.customerId)))
> {
>             return false;
>         }
>         return true;
>     }
>
>     @Override
>     public String toString() {
>         return
> "com.entity.customer.Customer[customerId=" +
> customerId + "]";
>     }
>
> }
>  and also the implementation in my customerDAO is coming
> from
>  the CustomerFacade which has this method
>
>  public Customer find(Object id) {
>         return em.find(Customer.class,
> id);
>     }
> and it has an implementation in the CustomerFacadeLocal
> interface as
>
> Customer find(Object id);
>
> So I created an object of the CustomerFacadeLocal in the
> managedbean of the page as
>
> private CustomerFacadeLocal customerDAO;
>
> and finally was trying to use the customerDAO to implement
> the find method.
> it works fine for the customerDAO.create(customer) and
> customerDAO.edit(customer) method using the following code
> snippet
> public String save_action() {
>         Customer customer = (Customer)
> getValue("#{Customer}");
>         if(customer.getCustomerId() ==
> null)
>         {
>         customerDAO.create(customer);
>         }
>      
>    else{
>         customerDAO.edit(customer);
>         }
>         return "success";
>     }.
>
> Please am very sorry for placing too much codes . was just
> trying to get you guys to get a thorough understand of where
> i am coming from .
>
> Thanks very much for your reply
>
>
> --- On Mon, 10/19/09, John Yeary <johnyeary@...>
> wrote:
>
> > From: John Yeary <johnyeary@...>
> > Subject: Re: [nbj2ee] JPA Find Method gives null
> value
> > To: nbj2ee@...
> > Date: Monday, October 19, 2009, 11:58 PM
> > I agree with Annabel, you need to look at
> > your implementation. Do you really
>  use an Object, or is your
> > key really something else? See the example entity
> below.
> >
> > @Entity
> > @Table(name = "CUSTOMER")
> > public class Customer implements Serializable {
> >
> >
> >     private static final long
> serialVersionUID = 1L;
> >     @Id
> >     @Basic(optional = false)
> >     @Column(name =
> "CUSTOMER_ID")
> >     private Integer customerId;
> >
> >     public Customer() {
> >     }
> >
> >
> >     public Customer(Integer customerId)
> {
> >        
> this.customerId = customerId;
> >     }
> >
> >     public Integer getCustomerId() {
> >         return
> customerId;
> >     }
> >
> >
>      public void setCustomerId(Integer
> customerId) {
> >
> >        
> this.customerId = customerId;
> >     }
> > }
> >
> >
> >
> > On Mon, Oct 19, 2009 at 3:45 PM,
> > Melongo Annabel <melongo_annabel@...>
> > wrote:
> >
> > What's the
> > implementation of your customerDAO.find in (
> customerDAOImpl
> > ?)
> >
> >
> >
> > From:
> > omoz4real <omoz4real@...>
> > To: nbj2ee@...
> >
> > Sent: Mon,
> > October 19, 2009 2:17:16 PM
> > Subject:
> > [nbj2ee]  JPA Find Method gives null value
> >
> >
>
> >
> > Hi John,
> >
> >
> >
> > When I try using the procedure u gave i.e.
> >
> >
> >
> > Customers customers = null;
> >
> > customers = customerDAO.find(Integer customerId);
> >
> >
> >
> > I get an error message from the IDE which says cannot
> find
> > symbol. symbol: variable Integer.
> >
> >
> >
> >
> > Also when I try it this way
> >
> > customers = customerDAO.find(new Integer(0));
> >
> >
> >
> > the return value is still a null value  and i
> have got
> > data in the database already. I am testing the output
> of the
> > application with
> >
> >
> >
> >
> > public String search_action() {
> >
> >        
> >
> >        
> >
> >       customers = customerDAO.find(new
> Integer(0));
> >
>
> >       if(customers != null)
> >
> >       {
> >
> >      
> System.out.println(customers.getFirstName());
> >
> >
> >       }
> >
> >       else{
> >
> >      
> >      System.out.println("GOT
> YA");
> >
> >       }
> >
> >       
> >
> >         return null;
> >
> >     }
> >
> >
> >
> >
> >
> > Thanks for your reply .please is there any another way
> I
> > can go about searching/querying the data in the
> database .
> >
> >
> >
> >
> >
> >
> >
> >
> >     
> >
> >
> > --
> > John Yeary
> > --
> > http://javaevangelist.blogspot.com
> >
> > "Far better it is to dare mighty things, to win
> > glorious triumphs, even though checkered by failure,
> than to
> > take rank with those poor spirits who neither enjoy
> much nor
> > suffer much, because they live in the grey twilight
> that
> > knows not victory nor defeat."
> >
> > -- Theodore Roosevelt
> >
> >
>
>
>
>
>
>
>






--
John Yeary
--
http://javaevangelist.blogspot.com

"Far better it is to dare mighty things, to win glorious triumphs, even though checkered by failure, than to take rank with those poor spirits who neither enjoy much nor suffer much, because they live in the grey twilight that knows not victory nor defeat."
-- Theodore Roosevelt



--
John Yeary
--
http://javaevangelist.blogspot.com

"Far better it is to dare mighty things, to win glorious triumphs, even though checkered by failure, than to take rank with those poor spirits who neither enjoy much nor suffer much, because they live in the grey twilight that knows not victory nor defeat."
-- Theodore Roosevelt

 « Return to Thread: JPA Find Method gives null value