In this example you will learn how to map one-to-one relationship using Hibernate. Consider the following relationship between Student and Address entity.
To create this relationship you need to have a STUDENT and ADDRESS table. The relational model is shown below.
Student.hbm.xml is used to create the STUDENT table.
01.xml version="1.0"?>02.03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"04."http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">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. <many-to-one name="studentAddress" class="com.vaannila.student.Address" column="STUDENT_ADDRESS" not-null="true" cascade="all" unique="true" />13. class>14.hibernate-mapping>Address.hbm.xml is used to create the ADDRESS table.
01.xml version="1.0"?>02.03."-//Hibernate/Hibernate Mapping DTD 3.0//EN"04."http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">05.<hibernate-mapping>06. <class name="com.vaannila.student.Address" table="ADDRESS">07. <meta attribute="class-description">This class contains the student's address08. details.meta>09. <id name="addressId" type="long" column="ADDRESS_ID">10. <generator class="native" />11. id>12. <property name="street" column="ADDRESS_STREET" type="string" length="250" />13. <property name="city" column="ADDRESS_CITY" type="string" length="50" />14. <property name="state" column="ADDRESS_STATE" type="string" length="50" />15. <property name="zipcode" column="ADDRESS_ZIPCODE" type="string" length="10" />16. class>17.hibernate-mapping>01."1.0" encoding="UTF-8"?>02.03. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"05.06. 07. "hibernate.connection.driver_class"> org.hsqldb.jdbcDriver 08. "hibernate.connection.url"> jdbc:hsqldb:hsql://localhost09. "hibernate.connection.username">sa10. "connection.password">11. "connection.pool_size">112. "hibernate.dialect"> org.hibernate.dialect.HSQLDialect 13. "show_sql">true14. "hbm2ddl.auto">create-drop15. "com/vaannila/student/Student.hbm.xml"/>16. "com/vaannila/student/Address.hbm.xml"/>17. 18.The following classes will be generated.
01.package com.vaannila.student;02. 03.// Generated Sep 3, 2009 7:20:37 PM by Hibernate Tools 3.2.4.GA04. 05./**06. * This class contains student details.07. */08.public class Student implements java.io.Serializable {09. 10. private long studentId;11. private String studentName;12. private Address studentAddress;13. 14. public Student() {15. }16. 17. public Student(String studentName, Address studentAddress) {18. this.studentName = studentName;19. this.studentAddress = studentAddress;20. }21. 22. public long getStudentId() {23. return this.studentId;24. }25. 26. public void setStudentId(long studentId) {27. this.studentId = studentId;28. }29. 30. public String getStudentName() {31. return this.studentName;32. }33. 34. public void setStudentName(String studentName) {35. this.studentName = studentName;36. }37. 38. public Address getStudentAddress() {39. return this.studentAddress;40. }41. 42. public void setStudentAddress(Address studentAddress) {43. this.studentAddress = studentAddress;44. }45. 46.}01.package com.vaannila.student;02. 03.// Generated Sep 3, 2009 7:20:37 PM by Hibernate Tools 3.2.4.GA04. 05./**06. * This class contains the student's address07. * details.08. */09.public class Address implements java.io.Serializable {10. 11. private long addressId;12. private String street;13. private String city;14. private String state;15. private String zipcode;16. 17. public Address() {18. }19. 20. public Address(String street, String city, String state, String zipcode) {21. this.street = street;22. this.city = city;23. this.state = state;24. this.zipcode = zipcode;25. }26. 27. public long getAddressId() {28. return this.addressId;29. }30. 31. public void setAddressId(long addressId) {32. this.addressId = addressId;33. }34. 35. public String getStreet() {36. return this.street;37. }38. 39. public void setStreet(String street) {40. this.street = street;41. }42. 43. public String getCity() {44. return this.city;45. }46. 47. public void setCity(String city) {48. this.city = city;49. }50. 51. public String getState() {52. return this.state;53. }54. 55. public void setState(String state) {56. this.state = state;57. }58. 59. public String getZipcode() {60. return this.zipcode;61. }62. 63. public void setZipcode(String zipcode) {64. this.zipcode = zipcode;65. }66. 67.}01.package com.vaannila.student;02. 03.import org.hibernate.HibernateException;04.import org.hibernate.Session;05.import org.hibernate.Transaction;06. 07.import com.vaannila.util.HibernateUtil;08. 09.public class Main {10. 11. public static void main(String[] args) {12. Session session = HibernateUtil.getSessionFactory().openSession();13. Transaction transaction = null;14. try {15. transaction = session.beginTransaction();16. Address address1 = new Address("OMR Road", "Chennai", "TN", "600097");17. Address address2 = new Address("Ring Road", "Banglore", "Karnataka", "560000");18. Student student1 = new Student("Eswar", address1);19. Student student2 = new Student("Joe", address2);20. session.save(student1);21. session.save(student2);22. transaction.commit();23. } catch (HibernateException e) {24. transaction.rollback();25. e.printStackTrace();26. } finally {27. session.close();28. }29. 30. }31. 32.}The Student table has two records.
The folder structure of the example is shown below.