Hibernate更新表

时间:2011-07-05 09:31:54

标签: hibernate hibernate-mapping

我是Hibernate的新手,并且按照http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html的教程,我对该教程中的主要课程有疑问。

我理解

Set phoneNumbers = new HashSet();
phoneNumbers.add(new Phone("house","32354353"));
phoneNumbers.add(new Phone("mobile","9889343423")); 
Student student = new Student("Eswar", phoneNumbers);
session.save(student);
transaction.commit();

将新记录插入三个表中。但如果这个学生得到另一个号码

Phone work = new Phone("mobile","12345678");

我如何更新PHONE和STUDENT_PHONE表格。感谢。

1 个答案:

答案 0 :(得分:0)

你遗漏了几件事。这段代码可以使用:

Set phoneNumbers = new HashSet();
student.setPhoneNumbers(phoneNumbers); // Add the phone numbers to the Student

Phone phone = new Phone("house","32354353");
session.save(phone); // Persist the new object
phoneNumbers.add(phone);

Phone phone = new Phone("mobile","9889343423");
session.save(phone); // Persist the new object
phoneNumbers.add(phone);

Student student = new Student("Eswar", phoneNumbers);
session.save(student);
transaction.commit();