因此,即使我使用了级联,也会遇到异常 java.lang.IllegalStateException:org.hibernate.TransientObjectException:
考虑到我在表保存数据之间的不同关系上使用级联这一事实不应该引起异常。但是确实如此,因此需要帮助
这是我的代码-
Instructor.java
@Entity
@Table(name="instructor")
public class Instructor {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String emailID;
@OneToOne(cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
@JoinColumn(name="instructor_detail_id")
private InstructorDetail instructorDetailID;
@OneToMany(mappedBy="instructorId",cascade={CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
private List<Courses> courses;
}
InstructorDetail.java
@Entity
@Table(name="instructor_detail")
public class InstructorDetail {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="youtube_channel")
private String youtubeChannel;
@Column(name="hobby")
private String hobby;
@OneToOne(mappedBy="instructorDetailID", cascade={CascadeType.DETACH,CascadeType.MERGE,CascadeType.REFRESH,CascadeType.PERSIST})
private Instructor instructor;
}
Courses.java
@Entity
@Table(name="course")
public class Courses {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="title")
private String title;
@ManyToOne(cascade={CascadeType.DETACH, CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH,})
@JoinColumn(name="instructor_id")
private Instructor instructorId;
}
createRecordDemo.java
public class createRecordDemo {
public static void main(String[] args) {
//Build Session Factory
SessionFactory factory=new Configuration().configure("hibernate.cfg.xml").addAnnotatedClass(Instructor.class)
.addAnnotatedClass(InstructorDetail.class).addAnnotatedClass(Courses.class).buildSessionFactory();
//get current session
Session session= factory.getCurrentSession();
try {
//begin transaction
session.beginTransaction();
Instructor instructor= new Instructor("Chad","Darby","chadDarby@luv2Code.com");
Instructor instructor2= new Instructor("Bharath","Thippireddy","bharath@gmail.com");
Instructor instructor3= new Instructor("Deepali","Srivastava","deepali@gmail.com");
InstructorDetail instructorDetail= new InstructorDetail("luv2Code","Teaching");
InstructorDetail insDetail2=new InstructorDetail("DS&Algo","Problem Solving");
InstructorDetail insDetail3= new InstructorDetail("webServices","communication");
instructor.setInstructorDetailID(instructorDetail);
instructor2.setInstructorDetailID(insDetail2);
instructor3.setInstructorDetailID(insDetail3);
Courses course1= new Courses("Spring");
Courses course2= new Courses("Hibernate");
Courses course3= new Courses("DataStructures and Algorithm");
Courses course4= new Courses("Web services- SOAP & REST");
course1.setInstructorId(instructor);
course2.setInstructorId(instructor);
course3.setInstructorId(instructor2);
course4.setInstructorId(instructor3);
session.save(course1);
session.save(course2);
session.save(course3);
session.save(course4);
session.getTransaction().commit();
} catch (Exception e) {
System.out.println("Exception Occured"+e);
e.printStackTrace();
}finally {
session.close();
factory.close();
}
}
}
我确实知道克服给定异常的一种方法是在课程对象之前保存教师对象。但是我想让冬眠处理它。我没有使用CascadeType.ALL,因为如果课程被删除,我不希望删除教师详细信息。
我是该框架的新手,我喜欢学习它。我已经尝试过回答类似问题,但是我想知道我的代码在哪里失败
error-INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Hibernate: insert into course (instructor_id, title) values (?, ?)
Hibernate: insert into course (instructor_id, title) values (?, ?)
Hibernate: insert into course (instructor_id, title) values (?, ?)
Hibernate: insert into course (instructor_id, title) values (?, ?)
Exception Occuredjava.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: entity.Instructor
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: entity.Instructor