开发者

Hibernate update table

开发者 https://www.devze.com 2023-03-17 20:58 出处:网络
I开发者_StackOverflow中文版 am new to Hibernate and following the tutorial at http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html, I have a quest

I开发者_StackOverflow中文版 am new to Hibernate and following the tutorial at http://www.vaannila.com/hibernate/hibernate-example/hibernate-mapping-one-to-many-using-annotations-1.html, I have a question about the main class in that tutorial.

I understand that

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();

insert new records into three tables. But if this student gets another number

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

How could I update the PHONE and STUDENT_PHONE tables. Thanks.


You are missing a couple of things. This code will work:

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消