我已经看过- Save child objects automatically using JPA Hibernate
Hibernate: OneToMany save children by cascade
我有下表-
我正在尝试-在MainApp中保存(instructorDetail),并希望自动保存相关的讲师和课程
这是我的课程-
@Entity
@Table(name="instructor_detail")
public class InstructorDetail {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="youtube_channel")
private String youtubeChannel;
@Column(name="hobby")
private String hobby;
@OneToOne(mappedBy = "instructorDetail", cascade= {
CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH
})
private Instructor instructor;
//getters and setters
}
@Entity
@Table(name="instructor")
public class Instructor {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="email")
private String email;
@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name="instructor_detail_id")
private InstructorDetail instructorDetail;
@OneToMany(mappedBy="instructor",
cascade =
{CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
private List<Course> courses;
//...getters and setters
//one more method to add courses
public void addCourse(Course c) {
if(courses == null) {
courses = new ArrayList<>();
}
courses.add(c);
c.setInstructor(this);
}
}
@Entity
@Table(name="course")
public class Course {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
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 instructor;
//getters and setters
}
//MainClass -
public class OneToManyBiDirectionalDemo {
public static void main(String[] args) {
Course c1 = new Course("Multithreading");
Course c2 = new Course("WebDevelopment");
Instructor ins1 = new Instructor("John","Purcell","john.purcell@email.com");
InstructorDetail insDet1 = new InstructorDetail("com.john.purcell","teaching");
insDet1.setInstructor(ins1);
ins1.setInstructorDetail(insDet1);
ins1.addCourse(c1);
ins1.addCourse(c2);
SessionFactory factory = new Configuration().
configure("hibernate.cfg.xml"). addAnnotatedClass(Course.class). addAnnotatedClass(Instructor.class). addAnnotatedClass(InstructorDetail.class). buildSessionFactory();
Session session = factory.getCurrentSession();
session.beginTransaction();
session.save(insDet1);
session.getTransaction().commit();
session.close();
factory.close();
}
}
我的期望是-在MainClass中调用save(instructorDetail)时,教师和课程也应自动保存。但是,执行当前操作只会节省教师的详细信息,而不会节省教师和课程。在日志中仅触发了一个插入,并且没有生成异常。我是Hibernate的新手,所以我的期望或理解可能不正确。可以吗?
答案 0 :(得分:1)
您配置的JPA CascadeType
(DETACH
,MERGE
,PERSIST
,REFRESH
)仅在与Session
一起使用时具有级联作用的evict()
,merge()
,persist()
,refresh()
。 (请参见this)
但是您正在使用session.save()
保存对象。这就是为什么它没有任何级联效果的原因。更改为以下任一选项应该可以解决问题:
(1)将所有CascadeType
更改为ALL
。
(2)使用session.persist()
插入记录:
session.persist(insDet1);