Adam Smith Adam Smith - 1 year ago 123
Java Question

oracle SQL Hibernate one-to-one mapping returns NullPointerException

I am developing an application and using Hibernate first time. I am trying to implement 1-2-1 mapping but it results in NullPointerException. I am using Oracle as backend.

Student Class

public class Student_Info {

@SequenceGenerator(name="sid", sequenceName="ids", initialValue=1, allocationSize=1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="sid")
private int rollNo;

private String sname;

public int getRollNo() {
return rollNo;
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
public String getSname() {
return sname;
public void setSname(String sname) {
this.sname = sname;

Student_detail Class

public class Student_detail {
@GeneratedValue(generator="newGenerator")//name of the primary key generator
@GenericGenerator(name="newGenerator", strategy="foreign", parameters={@Parameter(value="Student_Info", name="property")})
private int rollno;
private String phone;

@OneToOne(cascade = CascadeType.ALL)
private Student_Info student_Info;

public int getRollno() {
return rollno;

public void setRollno(int rollno) {
this.rollno = rollno;

public String getPhone() {
return phone;

public void setPhone(String phone) { = phone;

public Student_Info getStudent_Info() {
return student_Info;

public void setStudent_Info(Student_Info student_Info) {
this.student_Info = student_Info;

Main Class

Student_Info student_Info = new Student_Info();
student_Info.setSname("Vivek Singh");

Student_detail detail = new Student_detail();

SessionFactory factory = new Configuration().configure("hibernate/hibernate.cfg.xml").buildSessionFactory();
Session session = factory.openSession();



Exception in thread "main" java.lang.NullPointerException
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(
at org.hibernate.internal.SessionImpl.fireSave(
at com.hiber.Main.main(

Tables are already there in DB with foreign key. one more thing I am using sequence to generate id, so I think it is causing the problem, I am not sure.

Answer Source
@GenericGenerator(name="newGenerator", strategy="foreign", parameters={@Parameter(value="Student_Info", name="property")})

Here you specified like value = "Student_Info" which is invalid in this mapping. you have to specify like value = "student_Info".(name of value not the name of class ). This fixed it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download