org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
I am trying one to one relationships using annotations. But getting below
exception
java.lang.NullPointerException
at
org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
I googled it but all the suggestions I am getting, I have already written.
This is the code.
package test.hibernate;
@Entity
@Table(name="EMPLOYEE")
public class Employee {
@Id
@GeneratedValue
@Column(name="EMP_ID")
private Long empID;
@Column(name="fName")
private String fName;
@Column(name="lName")
private String lName;
@OneToOne(mappedBy="employee", cascade=CascadeType.ALL)
private Address address;
public Employee(/* Long empID, */String fName, String lName) {
super();
// this.empID = empID;
this.fName = fName;
this.lName = lName;
}
public Employee() {
super();
}
public Long getEmpID() {
return empID;
}
public void setEmpID(Long empID) {
this.empID = empID;
}
public String getfName() {
return fName;
}
public void setfName(String fName) {
this.fName = fName;
}
public String getlName() {
return lName;
}
public void setlName(String lName) {
this.lName = lName;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
}
`
Address.java:
`
package test.hibernate;
@NamedNativeQueries({ @NamedNativeQuery(name = "addressQuery",
query = "from Address", resultClass = Address.c
lass) })
@Entity
@Table(name="ADDRESS")
public class Address {
@Id
@Column(name = "EMP_ID")
@GenericGenerator(name = "gen", strategy = "foreign", parameters =
@Parameter(name = "property", value = "employee"))
private Long empID;
@Column(name = "ADDRESS")
private String address;
@Column(name = "CITY")
private String city;
@OneToOne
@PrimaryKeyJoinColumn
private Employee employee;
public Address() {
super();
// TODO Auto-generated constructor stub
}
public Address(/* Long empID, */String address, String city) {
super();
// this.empID = empID;
this.address = address;
this.city = city;
}
public Long getEmpID() {
return empID;
}
public void setEmpID(Long empID) {
this.empID = empID;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
/*
* @Override public String toString() { return "Address [empID=" +
empID +
* ", address=" + address + ", city=" + city + ", employee=" +
employee +
* "]"; }
*/
}
`
And main class:
public class Test {
public static void main(String[] args) {
System.out.println("Hibernate one to many (XML Mapping)");
Session session = HibernateUtil.openSession();
session.beginTransaction();
Department department = new Department();
department.setDepartmentName("IT");
session.save(department);
Employee emp1 = new Employee("aniket", "Deshmukh");
Address addr = new Address("Osmanpura", "aurangabad");
emp1.setAddress(addr);
addr.setEmployee(emp1);
Employee emp2 = new Employee(/*new Long("4"),*/"Yogesh", "Gaikawad");
emp1.setDepartment(department);
emp2.setDepartment(department);
session.save(emp1);
session.save(emp2);
@SuppressWarnings("unchecked")
List<Employee> employees = session.createQuery("from
Employee").list();
for(Employee e:employees){
System.out.println(e);
}
session.getTransaction().commit();
System.out.println("Done");
}
}
I am getting the following exception:
java.lang.NullPointerException
at
org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:135)
at
org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1127)
at
org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at
org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1283)
at test.hibernate.HibernateUtil.openSession(HibernateUtil.java:14)
at test.hibernate.Test.main(Test.java:11)
What is wrong with my code? Can someone help?
No comments:
Post a Comment