-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: java.lang.NullPointerException onetomany-manytoone
PostPosted: Fri May 30, 2008 1:11 pm 
Newbie

Joined: Thu May 22, 2008 2:16 pm
Posts: 9
I am new to Hibernate.(just 2 days) . I am trying to add two addressess for my Customer object.

I have two Tables Customer and CustomerAddresses
There is one to many relationship from Customer table's side and
Manytoone relationship from CustomerAddresses side.

When I attempted to persist two CustomersAddress for a Customer, I got the Nullpointer exception. I belive that I can't get the Customer, but I have no idea at this point to solve that. Here are my codes;

I am testing it with a JSF web application.

Thank you for precious solutions...

Customer.java

===============================================

Code:
package com.acmecargo.Entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity
@Table(name = "CUSTOMERS")
public class Customer implements Serializable
{
   
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CUSTOMERS_SEQ")
   @SequenceGenerator(name="CUSTOMERS_SEQ", sequenceName="CUSTOMERS_SEQ")
   @Column (name="CUSTOMERID")
   private int customerID;
   
   @Column (name="FIRSTNAME")
   private String firstname;
   
   @Column (name="LASTNAME")
   private String lastname;
   
   @Column (name="USERNAME")
   private String username;
   
   @Column (name="PASSWORD")
   private String password;
   
   @Column (name="TELEPHONENUMBER")
   private String telephoneNumber;
   
   @Column (name="SECRETQUESTION")
   private String secretQuestion;
   
   @Column (name="SECRETANSWER")
   private String secretAnswer;
   
   @Column (name="COMPANY")
   private String company;
   
   @Column (name="EMAIL")
   private String email;
   
   @Column (name="TYPE")
   private char type;
   
   @OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
    private List<CustomerAddress> customerAddresses;/*A customer has many addresses*/
   
   public List<CustomerAddress> getCustomerAddresses() {
      return customerAddresses;
   }
   
   public void setCustomerAddresses(List<CustomerAddress> customerAddresses) {
      this.customerAddresses = customerAddresses;
   }
   public Customer(){}

   public int getCustomerID() {
      return customerID;
   }

   public void setCustomerID(int customerID) {
      this.customerID = customerID;
   }

   public String getFirstname() {
      return firstname;
   }

   public void setFirstname(String firstname) {
      this.firstname = firstname;
   }

   public String getLastname() {
      return lastname;
   }

   public void setLastname(String lastname) {
      this.lastname = lastname;
   }

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getTelephoneNumber() {
      return telephoneNumber;
   }

   public void setTelephoneNumber(String telephoneNumber) {
      this.telephoneNumber = telephoneNumber;
   }

   public String getSecretQuestion() {
      return secretQuestion;
   }

   public void setSecretQuestion(String secretQuestion) {
      this.secretQuestion = secretQuestion;
   }

   public String getSecretAnswer() {
      return secretAnswer;
   }

   public void setSecretAnswer(String secretAnswer) {
      this.secretAnswer = secretAnswer;
   }

   public String getCompany() {
      return company;
   }

   public void setCompany(String company) {
      this.company = company;
   }

   public String getEmail() {
      return email;
   }

   public void setEmail(String email) {
      this.email = email;
   }

   public char getType() {
      return type;
   }

   public void setType(char type) {
      this.type = type;
   }
   
}

CustomerAddress.java
==============================================

Code:
package com.acmecargo.Entities;

import java.io.Serializable;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

@Entity()
@Table(name = "CUSTOMERADDRESSES")
public class CustomerAddress implements Serializable
{
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CUSTOMERADDRESSES_SEQ")
   @SequenceGenerator(name="CUSTOMERADDRESSES_SEQ", sequenceName="CUSTOMERADDRESSES_SEQ")
   @Column (name="CUSTOMERADDRESSID")
   private int customerAddressesID;
   
   @Column (name="ADDRESSLINE1")
   public String addressLine1;
   
   @Column (name="ADDRESSLINE2")
   public String addressLine2;
   
   @Column (name="ZIPCODE")
   public String zipcode;
   
   @Column (name="CITY")
   public String city;
   
   @Column (name="COUNTRY")
   public String country;
   
   @Column (name="HOMETELEPHONE")
   public String homeTelephone;
   
   @Column (name="MOBILEPHONE")
   public String mobilePhone;
   
   @Column (name="FAX")
   public String fax;
   
   @ManyToOne()
    @JoinColumn(name="ADDRESSCUSTOMERID")
   private Customer customer;
   
   public Customer getCustomer() { /*A customer address has belong to one customer*/
      return customer;
   }
   public void setCustomer(Customer customer) {
      this.customer = customer;
   }
   public CustomerAddress(){}

   public String getAddressLine1() {
      return addressLine1;
   }

   public void setAddressLine1(String addressLine1) {
      this.addressLine1 = addressLine1;
   }

   public String getAddressLine2() {
      return addressLine2;
   }

   public void setAddressLine2(String addressLine2) {
      this.addressLine2 = addressLine2;
   }

   public String getZipcode() {
      return zipcode;
   }

   public void setZipcode(String zipcode) {
      this.zipcode = zipcode;
   }

   public String getCity() {
      return city;
   }

   public void setCity(String city) {
      this.city = city;
   }

   public String getCountry() {
      return country;
   }

   public void setCountry(String country) {
      this.country = country;
   }

   public String getHomeTelephone() {
      return homeTelephone;
   }

   public void setHomeTelephone(String homeTelephone) {
      this.homeTelephone = homeTelephone;
   }

   public String getMobilePhone() {
      return mobilePhone;
   }

   public void setMobilePhone(String mobilePhone) {
      this.mobilePhone = mobilePhone;
   }

   public String getFax() {
      return fax;
   }

   public void setFax(String fax) {
      this.fax = fax;
   }

   public int getCustomerAddressesID() {
      return customerAddressesID;
   }

   public void setCustomerAddressesID(int customerAddressesID) {
      this.customerAddressesID = customerAddressesID;
   }

   
}

Test.java
===============================================

Code:
package com.acmecargo.PageBeans;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.acmecargo.Entities.Customer;
import com.acmecargo.Entities.CustomerAddress;
import com.acmecargo.HibernateUtils.SessionManager;

public class Tester
{

   public static String Test()
   {
      Session s = SessionManager.openNewSession();
      Transaction t = s.beginTransaction();
      
       Customer customer0 = new Customer();
   
       customer0.setFirstname("anut");
      
       s.persist(customer0);

       CustomerAddress caddress = new CustomerAddress();
      
       caddress.setAddressLine1("USA");

       CustomerAddress caddress2 = new CustomerAddress();
         
       caddress2.setAddressLine1("Ikinci evim istanbuldur");
   
       customer0.getCustomerAddresses().add(caddress);
       caddress.setCustomer(customer0);
      
       customer0.getCustomerAddresses().add(caddress2);
       caddress2.setCustomer(customer0);
      
      t.commit();
      s.close();
      
      return "success";
      
   }
}




error.......
20:05:26,125 INFO [STDOUT] Hibernate: select CUSTOMERS_SEQ.nextval from dual
20:05:26,359 FATAL [application] java.lang.NullPointerException
javax.faces.el.EvaluationException: java.lang.NullPointerException
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:91)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
at javax.faces.component.UICommand.broadcast(UICommand.java:383)
at org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:316)
at org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:291)
at org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:248)
at org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:461)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
at com.sun.faces.lifecycle.LifecycleImpl.execute

....


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 1:38 pm 
Newbie

Joined: Thu May 22, 2008 2:16 pm
Posts: 9
I have fixed nullpointer exception by initializing

Customer object List collection

@OneToMany(cascade=CascadeType.ALL, mappedBy="customer")
private List<CustomerAddress> customerAddresses =new ArrayList<CustomerAddress>();

but now I have another problem like that ?


Code:
javax.servlet.ServletException: #{TestBean.Test}: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
   javax.faces.webapp.FacesServlet.service(FacesServlet.java:256)
   org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:147)
   org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:276)
   org.ajax4jsf.Filter.doFilter(Filter.java:175)
   org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)


java.sql.BatchUpdateException: ORA-02291: integrity constraint (ADMIN.CUSTOMERADDRESSES_CUSTOME_FK1) violated - parent key not found

   oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
   oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:4373)
   org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
   org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
   org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
   org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
   org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
   org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
   org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
   org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
   org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
   com.acmecargo.PageBeans.Tester.Test(Tester.java:42)
   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   java.lang.reflect.Method.invoke(Unknown Source)
   org.apache.el.parser.AstValue.invoke(AstValue.java:131)
   org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:276)
   com.sun.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:68)
   javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:77)
   com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:91)
   javax.faces.component.UICommand.broadcast(UICommand.java:383)
   org.ajax4jsf.component.AjaxViewRoot.processEvents(AjaxViewRoot.java:316)
   org.ajax4jsf.component.AjaxViewRoot.broadcastEvents(AjaxViewRoot.java:291)
   org.ajax4jsf.component.AjaxViewRoot.processPhase(AjaxViewRoot.java:248)
   org.ajax4jsf.component.AjaxViewRoot.processApplication(AjaxViewRoot.java:461)
   com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:97)
   com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251)
   com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:117)
   javax.faces.webapp.FacesServlet.service(FacesServlet.java:244)
   org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:147)
   org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:276)



Any idea ? why ?
Is it because I had primary keys which generated by sequences


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 30, 2008 3:15 pm 
Newbie

Joined: Thu May 22, 2008 2:16 pm
Posts: 9
I have solved my problem. I hope this solution helps somebody.

The problem was I used oracle's trigger enabled sequences on both

Customer and Customer Addresseses tables

Code:
CREATE OR REPLACE TRIGGER "ADMIN"."CUSTOMERADDRESSES_TRG"
BEFORE INSERT ON CUSTOMERADDRESSES
FOR EACH ROW
BEGIN
  SELECT CUSTOMERADDRESSES_SEQ.NEXTVAL INTO :NEW.CUSTOMERADDRESSID FROM DUAL;
END;
/
ALTER TRIGGER "ADMIN"."CUSTOMERADDRESSES_TRG" ENABLE;


In this situation, my application server(JBOSS) can't get the right sequence.
You should disable this trigger so JBOSS can get sequence by its name.


Code:
ALTER TRIGGER "ADMIN"."CUSTOMERS_TRG" DISABLE;
ALTER TRIGGER "ADMIN"."CUSTOMERADDRESSES_TRG" DISABLE;



No it works and hibernate can find parent key now.

Hope this help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.