The Hibernate Many to One Mapping Annotation Example tutorial shows you how to use Hibernate Many-To-One Unidirectional mapping using annotation based configuration. The Many-To-One Unidirectional mapping said that one table has a foreign key column that referring the primary key of associated table.
We are taking an example of Student and ClassRoom relationship. This relationship said that a student registers one ClassRoom and one ClassRoom has many students.
Other interesting posts you may like
Create Database Table
We create two tables with these below scripts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
create table CLASS_ROOM ( class_room_id BIGINT NOT NULL AUTO_INCREMENT, name VARCHAR(30) NOT NULL, PRIMARY KEY (CLASS_ROOM_ID) ); create table STUDENT ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(100) NOT NULL, entering_date DATE NOT NULL, nationality TEXT NOT NULL, code VARCHAR(30) NOT NULL, class_room_id INT(11) NOT NULL, PRIMARY KEY (id), CONSTRAINT student_classroom FOREIGN KEY (class_room_id) REFERENCES CLASS_ROOM (class_room_id) ON UPDATE CASCADE ON DELETE CASCADE ); |
Noticed that STUDENT table contains a foreign key (here is class_room_id field) referring to a primary key (here is id field) of CLASS_ROOM table.
Create project directory structure
In this Hibernate Many to One Mapping Annotation Example tutorial, we will create java project with final directory structure.
Maven dependencies
We provide the required Hibernate and MySQL dependency in pom.xml like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javabycode.hibernate</groupId> <artifactId>ManyToOneMappingExample</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>ManyToOneMappingExample</name> <properties> <hibernate.version>4.3.11.Final</hibernate.version> <mysql.version>5.1.31</mysql.version> </properties> <dependencies> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> </dependencies> </project> |
Create Model classes
Model class Student and ClassRoom are simple POJO class. Here we are using class Student and ClassRoom with JPA annotations to map them to a database tables (these tables were created in above step).
Student Model class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
package com.javabycode.hibernate.model; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name = "STUDENT") public class Student implements Serializable { private static final long serialVersionUID = 6832006422622219737L; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "NAME", nullable = false) private String name; @Column(name = "ENTERING_DATE", nullable = false) private Date enteringDate; @Column(name = "NATIONALITY", nullable = false) private String nationality; @Column(name = "CODE", nullable = false) private String code; @ManyToOne(optional = false) @JoinColumn(name = "CLASS_ROOM_ID") private ClassRoom classRoom; public Student() { } public Student(String name, Date enteringDate, String nationality, String code) { this.name = name; this.enteringDate = enteringDate; this.nationality = nationality; this.code = code; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public void setCode(String code) { this.code = code; } public void setEnteringDate(Date enteringDate) { this.enteringDate = enteringDate; } public void setNationality(String nationality) { this.nationality = nationality; } public String getCode() { return code; } public Date getEnteringDate() { return enteringDate; } public String getNationality() { return nationality; } public void setClassRoom(ClassRoom classRoom) { this.classRoom = classRoom; } public ClassRoom getClassRoom() { return classRoom; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((code == null) ? 0 : code.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Student)) return false; Student other = (Student) obj; if (id != other.id) return false; if (code == null) { if (other.code != null) return false; } else if (!code.equals(other.code)) return false; return true; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", enteringDate=" + enteringDate + ", nationality=" + nationality + ", code=" + code + ", classRoom=" + classRoom + "]"; } } |
ClassRoom Model class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.javabycode.hibernate.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "CLASS_ROOM") public class ClassRoom { @Id @GeneratedValue @Column(name = "CLASS_ROOM_ID") private long id; @Column(name = "NAME") private String name; public ClassRoom() { } public ClassRoom(String name) { this.name = name; } public long getId() { return id; } public void setId(long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "ClassRoom [id=" + id + ", name=" + name + "]"; } } |
Let’s dig deeper:
@ManyToOne annotation says that many Students refer to one ClassRoom and the attribute optional=false means this relationship becomes mandatory (no student row can be saved without a classRoom reference).
@JoinColumn annotation indicates that column CLASS_ROOM_ID in Student table will refer to primary key of the CLASS_ROOM table.
Create Hibernate configuration file
We need to provide for hiberate all stuffs like database dialect, driver class, url and account information to connect database. These stuffs is declared in file hibernate.cfg.xml like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">javabycode</property> <property name="hibernate.connection.password">mypassword</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/javabycode</property> <property name="show_sql">false</property> <property name="format_sql">false</property> <mapping class="com.javabycode.hibernate.model.Student"/> <mapping class="com.javabycode.hibernate.model.ClassRoom"/> </session-factory> </hibernate-configuration> |
This file is placed in src/main/resources folder.
Create Hibernate Utility class
For configuring hibernate on startup and managing session factory we create the HibernateUtil class like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javabycode.hibernate; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory; private static final ServiceRegistry serviceRegistry; static { try { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build(); sessionFactory = new Configuration().configure().buildSessionFactory(serviceRegistry); } catch (Throwable ex) { System.err.println("Session Factory could not be created." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } |
Create program to demonstrate Hibernate Many to One Mapping Annotation Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.javabycode.hibernate; import java.util.Date; import java.util.List; import org.hibernate.Session; import com.javabycode.hibernate.model.Student; import com.javabycode.hibernate.model.ClassRoom; public class HibernateExample { @SuppressWarnings("unchecked") public static void main(String[] args) { Student student1 = new Student("David Pham", new Date(), "USA", "1234566"); Student student2 = new Student("Bill Murray", new Date(), "USA", "1234567"); Student student3 = new Student("Steve Carell", new Date(), "USA", "1234568"); ClassRoom classRoom = new ClassRoom("Math"); student1.setClassRoom(classRoom); student2.setClassRoom(classRoom); student3.setClassRoom(classRoom); Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); session.persist(classRoom); session.persist(student1); session.persist(student2); session.persist(student3); List<Student> students = (List<Student>) session.createQuery("from Student ").list(); for (Student s : students) { System.out.println("Student Details : " + s); System.out.println("Student ClassRoom Details: " + s.getClassRoom()); } session.getTransaction().commit(); session.close(); } } |
As discussed above, STUDENT table contains the foreign key referring to a primary key of CLASS_ROOM table. So that we have to persisted ClassRoom entity firstly then we set student’s classRoom property and persist student.
Run main class and we get the output like below:
1 2 3 4 5 6 |
Student Details : Student [id=12, name=David Pham, enteringDate=Fri Oct 07 16:29:21 ICT 2016, nationality=USA, code=1234566, classRoom=ClassRoom [id=9, name=Math]] Student ClassRoom Details: ClassRoom [id=9, name=Math] Student Details : Student [id=13, name=Bill Murray, enteringDate=Fri Oct 07 16:29:21 ICT 2016, nationality=USA, code=1234567, classRoom=ClassRoom [id=9, name=Math]] Student ClassRoom Details: ClassRoom [id=9, name=Math] Student Details : Student [id=14, name=Steve Carell, enteringDate=Fri Oct 07 16:29:21 ICT 2016, nationality=USA, code=1234568, classRoom=ClassRoom [id=9, name=Math]] Student ClassRoom Details: ClassRoom [id=9, name=Math] |
That’s it on the Hibernate Many to One Mapping Annotation Example tutorial.
Download complete source code, please hit link below
ManyToOneMappingExample.zip (236 downloads)
Source code on Github https://github.com/javabycode/hibernate-many-to-one-mapping-annotation-example