@Test public void test2(){ Session session = HibernateUtils.getSession(); Transaction tx = session.beginTransaction(); Teacher t1=new Teacher("T001","张老师"); Teacher t2=new Teacher("T002","李老师"); Teacher t3=new Teacher("T003","王老师"); Teacher t4=new Teacher("T004","胡老师"); //创建学生类 Student stu1=new Student(2,"男","java",new Date(),"lg"); Student stu2=new Student(1,"女","java",new Date(),"lr"); Student stu3=new Student(3,"男","java",new Date(),"zs"); Student stu4=new Student(4,"女","java",new Date(),"lh"); //创建教师集合 Set<Teacher> s1=new HashSet<Teacher>(); s1.add(t1); s1.add(t2); s1.add(t3); Set<Teacher> s2=new HashSet<Teacher>(); s1.add(t1); s1.add(t4); s1.add(t3); Set<Teacher> s3=new HashSet<Teacher>(); s1.add(t1); s1.add(t2); s1.add(t4); Set<Teacher> s4=new HashSet<Teacher>(); s1.add(t3); s1.add(t2); s1.add(t4); stu1.setTeachers(s1); stu2.setTeachers(s2); stu3.setTeachers(s3); stu4.setTeachers(s4); session.save(t1); session.save(t2); session.save(t3); session.save(t4); session.save(stu1); session.save(stu2); session.save(stu3); session.save(stu4); tx.commit(); session.close(); }
1 回答
HansonQ
TA贡献223条经验 获得超56个赞
Set<Teacher> s1=new HashSet<Teacher>(); s1.add(t1);//这边是S1 s1.add(t2);//这边是S1 s1.add(t3);//这边是S1 Set<Teacher> s2=new HashSet<Teacher>(); s1.add(t1);//这边是S1,这边不应该是s2.add(t1)?依次类推 s1.add(t4);//这边是S1 s1.add(t3);//这边是S1 Set<Teacher> s3=new HashSet<Teacher>(); s1.add(t1);//这边是S1 s1.add(t2);//这边是S1 s1.add(t4);//这边是S1 Set<Teacher> s4=new HashSet<Teacher>(); s1.add(t3);//这边是S1 s1.add(t2);//这边是S1 s1.add(t4);//这边是S1
添加回答
举报
0/150
提交
取消