|
View:
New views
13 Messages
—
Rating Filter:
Alert me
|
|
|
JPA Find Method gives null valueHi All,
Please I am trying to implement the JPA find method in a visual web application so that when an action is performed , the application uses the Customer find(Object id); method to query the a database/entity class and returns the corresponding value. I have implemented this action in my application but I seem to get an Exception Details: java.lang.NullPointerException null error details whenever i click on the button to test the application. below is my code i have written so far to achieve this Customer customers = customerDAO.find(customer.getCustomerId()); where the customerId is the primary key of the entity class and customerId also the primary key of the database where the entity class was generated from I have the @EJB private CustomerFacadeLocal customerDAO; private Customer customer = null; on the managed bean class . please can anyone help me on this or alternative guide me on how i can achieve this using the find method of the entity/JPA class. Or alternatively refer me to a page or an article where the JPA/Entity find method has been implemented succesfully. Thanks in advance. I would really appreciate this. |
|
|
|
Re: JPA Find Method gives null valueI usually am a little more specific on the find. For example your find method is using an Object. I have customers which have either String, or Integer. So I use that more specific find method for example find(Integer customerId). The result is very easy to test especially when using @WebService to check the logic via a test call.
On Sun, Oct 18, 2009 at 7:52 PM, omoz4real <omoz4real@...> wrote: Hi All, -- 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 |
|
|
|
JPA Find Method gives null valueHi 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 . |
|
|
|
Re: JPA Find Method gives null valueWhat'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 . |
|
|
|
Re: JPA Find Method gives null valueI 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:
-- 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 |
|
|
|
Re: JPA Find Method gives null valueHi,
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 > > |
|
|
|
JPA Find Method gives null valueHi,
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 |
|
|
|
Re: JPA Find Method gives null valueIn 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 > -- > > "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 > > |
|
|
|
Re: JPA Find Method gives null valueThe 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 > > > > > > > > > > > |
|
|
|
Re: JPA Find Method gives null valueI 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 -- 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 |
|
|
|
Re: JPA Find Method gives null valueomos 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. -- 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 |
|
|
|
|
Hi John,
Thanks for your application. I have tried running the application and it runs successfully, but when i try testing it with the web service tester with known values from the sample database such as 1 for the find() and JumboCom for the findByName() method it gives me an error such as Service invocation threw an exception with message : null; Refer to the server log for more details Exceptions details : java.lang.reflect.InvocationTargetException
although i am running it from netbeans 6.5 but dont really think thats the problem. from your code you used @WebMethod and @WebParam(name = "customerId") for your Customer find and List<Customer> findByName method. I guess it was because it was a web service. the application i am developing is an enterprise application which consist of a web application and an ejb module and would want to test the find method from the web application.
What do i replace the @WebMethod and @WebParam(name) with. should it be @EJB and @EJBParam - just guessing though cos dont really know much about web services
@WebMethod // dont understand the words highlighted in bold pls
public Customer find(@WebParam(name = "customerId") Integer id) { return em.find(Customer.class, id); } @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 Tue, 10/20/09, John Yeary <johnyeary@...> wrote: > From: John Yeary <johnyeary@...> > Subject: Re: [nbj2ee] JPA Find Method gives null value > To: nbj2ee@... > Date: Tuesday, October 20, 2009, 5:17 AM > 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 > > |
Hi John,
Thanks for your application. I have tried running the application and it runs successfully, but when i try testing it with the web service tester with known values from the sample database such as 1 for the find() and JumboCom for the findByName() method it gives me an error such as
Service invocation threw an exception with message : null; Refer to the server log for more detailsExceptions details : java.lang.reflect.InvocationTargetExceptionalthough i am running it from netbeans 6.5 but dont really think thats the problem. from your code you used @WebMethod and @WebParam(name = "customerId") for your Customer find and List<Customer> findByName method. I guess it was because it was a web service. the application i am developing is an enterprise application which consist of a web application and an ejb module and would want to test the find method from the web application.What do i replace the @WebMethod and @WebParam(name) with. should it be @EJB and @EJBParam - just guessing though cos dont really know much about web services@WebMethod // dont understand the words highlighted in bold pls
public Customer find(@WebParam(name = "customerId") Integer id) {
return em.find(Customer.class, id);
}@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();
}> Date: Tuesday, October 20, 2009, 5:17 AM
--- On Tue, 10/20/09, John Yeary <johnyeary@...> wrote:
> From: John Yeary <johnyeary@...>
> Subject: Re: [nbj2ee] JPA Find Method gives null value
> To: nbj2ee@...
> 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.> customerName);
>
> 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",
> 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> like
>
> > 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> right?
>
> > this:
>
> > public Customer find(Object id) {
>
> > return em.find(Customer.class, id);
>
> > }
>
> > If guess right, the Integer was the primarey key,
>
> > 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 => "Customer.findByLastName",
>
> > :firstName"), @NamedQuery(name =
>
> > "Customer.findByMiddleName", query =
> "SELECT
>
> > c FROM Customer c WHERE c.middleName =
> :middleName"),
>
> > @NamedQuery(name =
>
> > 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>> > setAddressCollection(Collection<Address>
> > getAddressCollection() {
>
> >
>
> > return addressCollection;
>
> > }
>
> >
>
> > public void
>>
>
> > addressCollection) {
>
> > this.addressCollection =
>
> > addressCollection;
>
> > }
>
> >
>
> > public Collection<Telephone>
>
> > getTelephoneCollection() {> > telephoneCollection) {
> > return telephoneCollection;
>
> > }
>
> >
>
> > public void
>
> > setTelephoneCollection(Collection<Telephone>
>>
>
> > this.telephoneCollection =
>
> > telephoneCollection;
>
> > }
>
> >
>
> > @Override> > public boolean
> > public int hashCode() {
>
> > int hash = 0;
>
> > hash += (customerId != null ?
>
> > customerId.hashCode() : 0);
>
> > return hash;
>
> > }
>
> >
>
> > @Override
>
>
> > 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
> > }
>
> > return true;
>
> > }
>
> >
>
> > @Override
>
> > public String toString() {
>> > }
>
> > "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);
>
> > }> > So I created an object of the CustomerFacadeLocal in
> > and it has an implementation in the
> CustomerFacadeLocal
>
> > interface as
>
> >
>
> > Customer find(Object id);
>
> >
>
> 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));> output
> > >
>
> > >
>
> > >
>
> > > the return value is still a null value and i
>
> > have got
>
> > > data in the database already. I am testing the>
>
> > of the
>
> > > application with
>
> > >
>
> > >
>
> > >
>
> > >
>
> > > public String search_action() {> > Integer(0));
> > >
>
> > >
>
> > >
>
> > >
>
> > >
>
> > > customers = customerDAO.find(new
>>
>
> > >
>
> >
>
> > > 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 .
>
> > >
>
> > >
>
> > >
>
> > >
>
> > >
>
> > >
>
> > >> > > http://javaevangelist.blogspot.com
> > >
>
> > >
>
> > >
>
> > >
>
> > > --
>
> > > John Yeary
>
> > > --
>
>
> > >
>
> > > "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
>
>
| Free embeddable forum powered by Nabble | Forum Help |