Search

Hibernate One-To-Many Mapping Example Part- 2


01.package com.vaannila.student;
02. 
03.// Generated Sep 3, 2009 8:47:06 PM by Hibernate Tools 3.2.4.GA
04. 
05./**
06.* This class contains student's phone number
07.*          details.
08.*/
09.public class Phone implements java.io.Serializable {
10. 
11.private long phoneId;
12.private String phoneType;
13.private String phoneNumber;
14. 
15.public Phone() {
16.}
17. 
18.public Phone(String phoneType, String phoneNumber) {
19.this.phoneType = phoneType;
20.this.phoneNumber = phoneNumber;
21.}
22. 
23.public long getPhoneId() {
24.return this.phoneId;
25.}
26. 
27.public void setPhoneId(long phoneId) {
28.this.phoneId = phoneId;
29.}
30. 
31.public String getPhoneType() {
32.return this.phoneType;
33.}
34. 
35.public void setPhoneType(String phoneType) {
36.this.phoneType = phoneType;
37.}
38. 
39.public String getPhoneNumber() {
40.return this.phoneNumber;
41.}
42. 
43.public void setPhoneNumber(String phoneNumber) {
44.this.phoneNumber = phoneNumber;
45.}
46. 
47.}

01.package com.vaannila.student;
02. 
03.import java.util.HashSet;
04.import java.util.Set;
05. 
06.import org.hibernate.HibernateException;
07.import org.hibernate.Session;
08.import org.hibernate.Transaction;
09. 
10.import com.vaannila.util.HibernateUtil;
11. 
12.public class Main {
13. 
14.public static void main(String[] args) {
15.Session session = HibernateUtil.getSessionFactory().openSession();
16.Transaction transaction = null;
17.try {
18.transaction = session.beginTransaction();
19. 
20.Set<Phone> phoneNumbers = new HashSet<Phone>();
21.phoneNumbers.add(new Phone("house","32354353"));
22.phoneNumbers.add(new Phone("mobile","9889343423"));
23. 
24.Student student = new Student("Eswar", phoneNumbers);
25.session.save(student);
26. 
27.transaction.commit();
28.catch (HibernateException e) {
29.transaction.rollback();
30.e.printStackTrace();
31.finally {
32.session.close();
33.}
34. 
35.}
36. 
37.}
Create the Main class to run the example.

01.package com.vaannila.student;
02. 
03.import java.util.HashSet;
04.import java.util.Set;
05. 
06.import org.hibernate.HibernateException;
07.import org.hibernate.Session;
08.import org.hibernate.Transaction;
09. 
10.import com.vaannila.util.HibernateUtil;
11. 
12.public class Main {
13. 
14.public static void main(String[] args) {
15.Session session = HibernateUtil.getSessionFactory().openSession();
16.Transaction transaction = null;
17.try {
18.transaction = session.beginTransaction();
19. 
20.Set<Phone> phoneNumbers = new HashSet<Phone>();
21.phoneNumbers.add(new Phone("house","32354353"));
22.phoneNumbers.add(new Phone("mobile","9889343423"));
23. 
24.Student student = new Student("Eswar", phoneNumbers);
25.session.save(student);
26. 
27.transaction.commit();
28.catch (HibernateException e) {
29.transaction.rollback();
30.e.printStackTrace();
31.finally {
32.session.close();
33.}
34. 
35.}
36. 
37.}
On executing the Main class you will see the following output.
The STUDENT table has one record.
 
The PHONE table has two records.
The STUDENT_PHONE table has two records to link the student and phone numbers.


A single student record points to two phone numbers, this illustrates the one-to-many mapping.
The folder structure of the example is shown below.

No comments:

Post a Comment