In this example you will learn how to map many-to-one relationship using Hibernate Annotations. 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 class is used to create the STUDENT table.
01.package com.vaannila.student;02. 03.import javax.persistence.CascadeType;04.import javax.persistence.Column;05.import javax.persistence.Entity;06.import javax.persistence.GeneratedValue;07.import javax.persistence.Id;08.import javax.persistence.ManyToOne;09.import javax.persistence.Table;10. 11.@Entity12.@Table(name = "STUDENT")13.public class Student {14. 15. private long studentId;16. private String studentName;17. private Address studentAddress;18. 19. public Student() {20. }21. 22. public Student(String studentName, Address studentAddress) {23. this.studentName = studentName;24. this.studentAddress = studentAddress;25. }26. 27. @Id28. @GeneratedValue29. @Column(name = "STUDENT_ID")30. public long getStudentId() {31. return this.studentId;32. }33. 34. public void setStudentId(long studentId) {35. this.studentId = studentId;36. }37. 38. @Column(name = "STUDENT_NAME", nullable = false, length = 100)39. public String getStudentName() {40. return this.studentName;41. }42. 43. public void setStudentName(String studentName) {44. this.studentName = studentName;45. }46. 47. @ManyToOne(cascade = CascadeType.ALL)48. public Address getStudentAddress() {49. return this.studentAddress;50. }51. 52. public void setStudentAddress(Address studentAddress) {53. this.studentAddress = studentAddress;54. }55. 56.}Address class is used to create the ADDRESS table.
01.package com.vaannila.student;02. 03.import javax.persistence.Column;04.import javax.persistence.Entity;05.import javax.persistence.GeneratedValue;06.import javax.persistence.Id;07.import javax.persistence.Table;08. 09.@Entity10.@Table(name = "ADDRESS")11.public class Address {12. 13. private long addressId;14. private String street;15. private String city;16. private String state;17. private String zipcode;18. 19. public Address() {20. }21. 22. public Address(String street, String city, String state, String zipcode) {23. this.street = street;24. this.city = city;25. this.state = state;26. this.zipcode = zipcode;27. }28. 29. @Id30. @GeneratedValue31. @Column(name = "ADDRESS_ID")32. public long getAddressId() {33. return this.addressId;34. }35. 36. public void setAddressId(long addressId) {37. this.addressId = addressId;38. }39. 40. @Column(name = "ADDRESS_STREET", nullable = false, length=250)41. public String getStreet() {42. return this.street;43. }44. 45. public void setStreet(String street) {46. this.street = street;47. }48. 49. @Column(name = "ADDRESS_CITY", nullable = false, length=50)50. public String getCity() {51. return this.city;52. }53. 54. public void setCity(String city) {55. this.city = city;56. }57. 58. @Column(name = "ADDRESS_STATE", nullable = false, length=50)59. public String getState() {60. return this.state;61. }62. 63. public void setState(String state) {64. this.state = state;65. }66. 67. @Column(name = "ADDRESS_ZIPCODE", nullable = false, length=10)68. public String getZipcode() {69. return this.zipcode;70. }71. 72. public void setZipcode(String zipcode) {73. this.zipcode = zipcode;74. }75. 76.}01."1.0" encoding="UTF-8"?>02.03. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"05.06. 07. "hibernate.connection.driver_class"> org.hsqldb.jdbcDriver08. "hibernate.connection.url"> jdbc:hsqldb:hsql://localhost09. "hibernate.connection.username">sa10. "connection.password">11. "connection.pool_size">112. "hibernate.dialect"> org.hibernate.dialect.HSQLDialect13. "show_sql">true14. "hbm2ddl.auto">create15. class="com.vaannila.student.Student" />16. class="com.vaannila.student.Address" />17. 18.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 address = new Address("OMR Road", "Chennai", "TN", "600097");17. //By using cascade=all option the address need not be saved explicitly when the student object is persisted the address will be automatically saved.18. //session.save(address);19. Student student1 = new Student("Eswar", address);20. Student student2 = new Student("Joe", address);21. session.save(student1);22. session.save(student2);23. transaction.commit();24. } catch (HibernateException e) {25. transaction.rollback();26. e.printStackTrace();27. } finally {28. session.close();29. }30. 31. }32. 33.}The Student table has two records.
The folder structure of the example is shown below.