为什么这两句话都要写。
为什么双向的代码都要写啊?我测试只写setgrade或者只写getStudents().add,数据库记录都是正常的啊?只写一种不行吗?
stu1.setGrade(gra);
stu2.setGrade(gra);
gra.getStudents().add(stu1);
gra.getStudents().add(stu2);
为什么双向的代码都要写啊?我测试只写setgrade或者只写getStudents().add,数据库记录都是正常的啊?只写一种不行吗?
stu1.setGrade(gra);
stu2.setGrade(gra);
gra.getStudents().add(stu1);
gra.getStudents().add(stu2);
2016-05-05
没有配置级联:
GradeEntity grade = new GradeEntity("国家6","性别"); StudentEntity student1 = new StudentEntity("美国6","男"); StudentEntity student2 = new StudentEntity("法国6","女"); //配置多对1 student1.setGrade(grade); student2.setGrade(grade); //没有配置级联,所以3个都要保存 session.save(grade); session.save(student1); session.save(student2);
配置了级联
GradeEntity grade = new GradeEntity("国家6","性别"); StudentEntity student1 = new StudentEntity("美国6","男"); StudentEntity student2 = new StudentEntity("法国6","女"); //1对多 grade.getStudent().add(student1); grade.getStudent().add(student2); //多对1 student1.setGrade(grade); student2.setGrade(grade); //配置级联后主要写save(grade)就好了,但是1对多的关系必须写 session.save(grade);
看起来就这个区别
举报