在线程" main"中获得异常我的hibernate程序的java.lang.NullPointerException

时间:2017-10-04 00:17:23

标签: java hibernate jpa

这是我的StudentDetails课程:

package com.gontuseries.hibernate;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

@Entity
@Table(name="STUDENT_DETAILS")
public class StudentDetails {
@Id @GeneratedValue( generator= "newGenerator")
@GenericGenerator(name="newGenerator",strategy = "foreign", parameters = { 
@Parameter(name="property",value="Student_Info")})
private int rollNo;

private int mobile_no;

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name ="rollNo")
private Student_Info student=new Student_Info();

public Student_Info getStudent() {
    return student;
}
public void setStudent(Student_Info student) {
    this.student = student;
}
public int getMobile_no() {
    return mobile_no;
}
public void setMobile_no(int mobile_no) {
    this.mobile_no = mobile_no;
}


public int getRollNo() {
    return rollNo;
}
public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
}
}

这是我的主要课程:

 package com.gontuseries.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.*;
public class Main {
public static void main(String[] args)
{

    Student_Info student=new Student_Info();
    student.setName("Gontu_Rajesh");
    StudentDetails sd=new StudentDetails();
    sd.setMobile_no(455656);
    sd.setStudent(student);
    // student.setRollNo(3);
    //Student_Info student2=new Student_Info();
    //student2.setName("Gontu_Rajesh_new name");

    SessionFactory sessionFactory= new 
    Configuration().configure().buildSessionFactory();
    Session session = sessionFactory.openSession();
    session.beginTransaction();

    session.save(sd);
    //session.save(student2);

    session.getTransaction().commit();
    session.close();
    sessionFactory.close();
}

}

这是我的Student_Info类:

package com.gontuseries.hibernate;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

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

@Id @GeneratedValue(strategy = GenerationType.SEQUENCE)
private int rollNo;

private String name;

public int getRollNo() {
    return rollNo;
}
public void setRollNo(int rollNo) {
    this.rollNo = rollNo;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

}

运行Main class后我得到了:

INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
Exception in thread "main" java.lang.NullPointerException
    at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:645)
    at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4725)
    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:691)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:683)
    at org.hibernate.internal.SessionImpl.save(SessionImpl.java:678)
    at com.gontuseries.hibernate.Main.main(Main.java:26)

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

你可能对这篇文章有同样/类似的问题: java.lang.NullPointerException at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:521)

尝试替换它:

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

用这个:

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

使用外国策略时,请记住使用属性名称而不是实体名称。该值应该是student而不是Student_Info