Adam Smith Adam Smith - 8 days ago 4
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

@Entity
@Table(name="student_info")
public class Student_Info {

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

@Column
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

@Entity
@Table(name="student_detail")
public class Student_detail {
@Id
@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)
@JoinColumn(name="rollno")
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) {
this.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();
detail.setPhone("2345678");
detail.setStudent_Info(student_Info);

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

session.save(detail);
session.getTransaction().commit();

session.close();
factory.close();


Error

Exception in thread "main" java.lang.NullPointerException
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:547)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4636)
at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:81)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:105)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:192)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:38)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:177)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:32)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:73)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:668)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:660)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:655)
at com.hiber.Main.main(Main.java:23)


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