In this example you will learn how to map one-to-many relationship using Hibernate. Consider the following relationship between Student and Phone entity.
According to the relationship a student can have any number of phone numbers.
To create this relationship you need to have a STUDENT, PHONE and STUDENT_PHONE table. The relational model is shown below.
To create the STUDENT and PHONE table you need to create the following hibernate mapping files.
Student.hbm.xml is used to create the STUDENT and STUDENT_PHONE table.
01.<?xml version="1.0"?>02.<!DOCTYPE hibernate-mapping PUBLIC03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"05.<hibernate-mapping>06. <class name="com.vaannila.student.Student" table="STUDENT">07. <meta attribute="class-description">This class contains student details.</meta>08. <id name="studentId" type="long" column="STUDENT_ID">09. <generator class="native" />10. </id>11. <property name="studentName" type="string" not-null="true" length="100" column="STUDENT_NAME" />12. <set name="studentPhoneNumbers" table="STUDENT_PHONE" cascade="all">13. <key column="STUDENT_ID" />14. <many-to-many column="PHONE_ID" unique="true" class="com.vaannila.student.Phone" />15. </set>16. </class>17.</hibernate-mapping>Phone.hbm.xml is used to create the PHONE table.
01.<?xml version="1.0"?>02.<!DOCTYPE hibernate-mapping PUBLIC03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"05.<hibernate-mapping>06. <class name="com.vaannila.student.Phone" table="PHONE">07. <meta attribute="class-description">This class contains student's phone number08. details.</meta>09. <id name="phoneId" type="long" column="PHONE_ID">10. <generator class="native" />11. </id>12. <property name="phoneType" type="string" length="10" column="PHONE_TYPE" />13. <property name="phoneNumber" type="string" length="15" column="PHONE_NUMBER" />14. </class>15.</hibernate-mapping>01.<?xml version="1.0" encoding="UTF-8"?>02.<!DOCTYPE hibernate-configuration PUBLIC03. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"05.<hibernate-configuration>06. <session-factory>07. <property name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver </property>08. <property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost </property>09. <property name="hibernate.connection.username">sa</property>10. <property name="connection.password"></property>11. <property name="connection.pool_size">1</property>12. <property name="hibernate.dialect">org.hibernate.dialect.HSQLDialect </property>13. <property name="show_sql">true</property>14. <property name="hbm2ddl.auto">create-drop</property>15. <mapping resource="com/vaannila/student/Student.hbm.xml"/>16. <mapping resource="com/vaannila/student/Phone.hbm.xml"/>17. </session-factory>18.</hibernate-configuration>The following classes will be generated.
01.package com.vaannila.student;02. 03.// Generated Sep 3, 2009 8:47:06 PM by Hibernate Tools 3.2.4.GA04. 05.import java.util.HashSet;06.import java.util.Set;07. 08./**09. * This class contains student details.10. */11.public class Student implements java.io.Serializable {12. 13. private long studentId;14. private String studentName;15. private Set<Phone> studentPhoneNumbers = new HashSet<Phone>(0);16. 17. public Student() {18. }19. 20. public Student(String studentName) {21. this.studentName = studentName;22. }23. 24. public Student(String studentName, Set<Phone> studentPhoneNumbers) {25. this.studentName = studentName;26. this.studentPhoneNumbers = studentPhoneNumbers;27. }28. 29. public long getStudentId() {30. return this.studentId;31. }32. 33. public void setStudentId(long studentId) {34. this.studentId = studentId;35. }36. 37. public String getStudentName() {38. return this.studentName;39. }40. 41. public void setStudentName(String studentName) {42. this.studentName = studentName;43. }44. 45. public Set<Phone> getStudentPhoneNumbers() {46. return this.studentPhoneNumbers;47. }48. 49. public void setStudentPhoneNumbers(Set<Phone> studentPhoneNumbers) {50. this.studentPhoneNumbers = studentPhoneNumbers;51. }52. 53.}01.package com.vaannila.student;02. 03.// Generated Sep 3, 2009 8:47:06 PM by Hibernate Tools 3.2.4.GA04. 05./**06. * This class contains student's phone number07. * 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.}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.}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.
A typical metadata ORM model is create by xml mapping style,alternatively we can use hibernate annotation to design our persistance class.
ReplyDeleteSee the below to example one with annotaion and one without it
http://fundapass.blogspot.in/2012/09/hibernate-with-annotation-example.html
http://fundapass.blogspot.in/2012/09/hibernate-one-to-many-mapping.html