hibernate级联保存,如果表中有这条记录会把这条记录修改,但是不级联保存提示违反约束
两张表teacher 和 course
级联保存的时候如果teacher里有id为't22'的记录,如果在插入一条't22'不报错,会把't22'修改
但是不级联,单独save一个id为't22'的记录的话会提示违反约束。为什么呀
public void insert(){
System.out.println("插入的是insert");
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
Teacher tea = new Teacher("t22", "老师222");
Course cou = new Course("c22", "课程", tea);
tea.getCourseSet().add(cou);
session.save(cou);
tr.commit();
session.close();
}
public void insertTeacher(){这样就会报错,为什么呀
System.out.println("插入的是Teacher");
Session session = sessionFactory.openSession();
Transaction tr = session.beginTransaction();
Teacher tea = new Teacher("t22", "老师222");
session.save(tea);
tr.commit();
session.close();
}