我在插入时遇到问题。我想在结果表中保存详细信息而不保存父表中的外键参数。 有三个pojo类:
@Entity
@Table(name="course")
public class Course implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column( name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@Column(name="course_id",nullable = false)
private String course_id;
@Column( name="course_name")
private String course_name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCourse_id() {
return course_id;
}
public void setCourse_id(String course_id) {
this.course_id = course_id;
}
public String getCourse_name() {
return course_name;
}
public void setCourse_name(String course_name) {
this.course_name = course_name;
}
**@OneToMany(fetch = FetchType.EAGER, mappedBy = "course")
private Set<Result> result = new HashSet<Result>(0);
public Set<Result> getResult() {
return this.result;
}
public void setResult(Set<Result> result) {
this.result = result;
}**
}
第二堂课是Student.java
@Entity
@Table(name ="student")
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
//Attribute----------------------------------
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
//Attribute----------------------------------
@Column(name="student_id", nullable=false)
private long student_id;
public long getStudent_id() {
return student_id;
}
public void setStudent_id(long student_id) {
this.student_id = student_id;
}
//Attribute----------------------------------
@Column(name="student_name")
private String student_name;
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
//Attribute----------------------------------
@Column(name="student_contact_number")
private long student_contact_number;
public long getStudent_contact_number() {
return student_contact_number;
}
public void setStudent_contact_number(long student_contact_number) {
this.student_contact_number = student_contact_number;
}
//This is for the foreign key element in the Result.java POJO class
@OneToMany(fetch = FetchType.LAZY,cascade = {CascadeType.ALL}, mappedBy = "student")
private Set<Result> result = new HashSet<Result>(0);
public Set<Result> getResult() {
return this.result;
}
public void setResult(Set<Result> result) {
this.result = result;
}
}
第三个是Result.java,它包含外键
@Entity
@Table(name="result")
public class Result implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@Column(name="id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="semester")
private int semester;
public int getSemester() {
return semester;
}
public void setSemester(int semester) {
this.semester = semester;
}
@Column(name="marks")
private int marks;
public int getMarks() {
return marks;
}
public void setMarks(int marks) {
this.marks = marks;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", nullable = false)
private Student student;
public Student getStudent() {
return this.student;
}
public void setStudent(Student student_id) {
this.student = student_id;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "course_id", nullable = false)
private Course course;
public Course getCourse() {
return this.course;
}
public void setCourse(Course course) {
this.course = course;
}
}
现在我用来插入的代码是这样的:
Session session;
Transaction t;
Query query;
Configuration cfg=new Configuration();
cfg.configure("hibernate.cfg.xml");
//@SuppressWarnings("deprecation")
SessionFactory factory=cfg.buildSessionFactory();
session=factory.openSession();
t=session.beginTransaction();
Result result;
Course cs;
Student st;
for(int i=0; i<jsrm.get(0).size(); i++)
{
result=new Result();
cs= new Course();
st=new Student();
cs.setCourse_id(jsrm.get(0).get(i).getcourse_id());
st.setStudent_id(Integer.parseInt(jsrm.get(0).get(i).getstudent_id()));
result.setSemester(Integer.parseInt(jsrm.get(0).get(i).getsemester()));
result.setMarks(Integer.parseInt(jsrm.get(0).get(i).getmarks()));
result.setCourse(cs);
result.setStudent(st);
session.save(result);
}
t.commit();//transaction is committed
session.close();
错误是
org.hibernate.TransientPropertyValueException:非null属性引用瞬态值 - 必须先保存瞬态实例 当前操作:Result.course - &gt;当然
有没有办法存储数据而不保存课程和学生。