为了账号安全,请及时绑定邮箱和手机立即绑定

用hibernate注解写了一个多对多的案例,为何中间表数据只插入了一部分?

用hibernate注解写了一个多对多的案例,为何中间表数据只插入了一部分?

广广0 2017-01-22 10:34:15
@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


查看完整回答
反对 回复 2017-02-07
  • 1 回答
  • 0 关注
  • 1504 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信