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
....