1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| public void add() { Session s = null; Transaction tx = null; try { Set<Teacher> ts = new HashSet<Teacher>();
Teacher t1 = new Teacher(); t1.setName("t1 name"); ts.add(t1);
Teacher t2 = new Teacher(); t2.setName("t2 name"); ts.add(t2);
Set<Student> ss = new HashSet<Student>(); Student s1 = new Student(); s1.setName("s1"); ss.add(s1);
Student s2 = new Student(); s2.setName("s2"); ss.add(s2);
t1.setStudents(ss); t2.setStudents(ss); s1.setTeachers(ts); s2.setTeachers(ts);
s = HibernateUtil.getSession(); tx = s.beginTransaction(); s.save(t1); s.save(t2); s.save(s1); s.save(s2); tx.commit(); } finally { if (s != null) s.close(); } }
|