Contents [hide ]
Many to One Mapping
Many To One mapping to represent a many to one relationship between tables. A many to one relationship occurs when many records from the main table correspond to only one record from the second table.
For example, many students can attend to the same course, so many records from the student table will correspond to the same record from the course table.
Tools and Technologies Used:
1. Java 8
2. MySQL 8
3. Eclipse IDE
4. mysql-connector-java-8.0.23.jar
5. Hibernate 5.5.7.Final
6. Maven 3.6.1
Maven Project for Many to One Mapping
We need to follow below steps:
Step 1: Click on File --> New --> Maven Project.
Step 2: Check Checkbox Create a simple project (skip archetype selection). Click on Next button.
Step 3: Enter Group Id, Artifact Id, Version, Packaging, Name, Description details and click on Finish button.
Update pom.xml file.
Open pom.xml file and paste below pom.xml file code.
4.0.0 com.hareeshsoni.blog.hibernate.mapping HibernateManyToOneExample 0.0.1-SNAPSHOT HibernateManyToOneExample Hibernate Many To One Example org.hibernate hibernate-core 5.5.7.Final mysql mysql-connector-java 8.0.23 org.apache.maven.plugins maven-compiler-plugin 3.6.1 1.8
Update Maven Project
Step 1: Right-click on project HibernateManyToOneExample --> Maven --> Update Project…
Step 2: Check Checkbox Force Update of Snapshots/Releases. Click on OK Button.
We have used the below JPA Annotations:
@Entity: @Entity annotation specifies that the class is an entity.
@Table: @Table annotation specifies the primary table for the annotated entity.
@Id: @Id annotation marks the particular field as the primary key of the Entity.
@GeneratedValue: This annotation is used to specify how the primary key should be generated.
@Column: This annotation maps the corresponding fields to their respective columns in the database table.
@JoinColumn: This annotation defines the foreign key. It is the column that associates the two tables.
@ManyToOne: This annotation is used to create many to one relationship between Student and Course entities.
package com.hareeshsoni.blog.hibernate.mapping.model; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "course") public class Course { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "course_id") private int courseId; @Column(name = "course_name") private String courseName; public Course() { } public Course(String courseName) { this.courseName = courseName; } public int getCourseId() { return courseId; } public void setCourseId(int courseId) { this.courseId = courseId; } public String getCourseName() { return courseName; } public void setCourseName(String courseName) { this.courseName = courseName; } }
package com.hareeshsoni.blog.hibernate.mapping.model; import javax.persistence.CascadeType; 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 { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "student_id") private int studentId; @Column(name = "student_name") private String studentName; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "course_id") private Course course; public Student() { } public Student(String studentName) { this.studentName = studentName; } public int getStudentId() { return studentId; } public void setStudentId(int studentId) { this.studentId = studentId; } public String getStudentName() { return studentName; } public void setStudentName(String studentName) { this.studentName = studentName; } public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } }
package com.hareeshsoni.blog.hibernate.mapping.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; private static SessionFactory buildSessionFactory() { Configuration configuration = new Configuration(); configuration.configure("hibernate.cfg.xml"); System.out.println("Hibernate configuration file loaded"); SessionFactory sessionFactory = configuration.buildSessionFactory(); return sessionFactory; } public static SessionFactory getSessionFactory() { if(sessionFactory == null) sessionFactory = buildSessionFactory(); return sessionFactory; } }
In hibernate.cfg.xml file provide correct username and password.
Create database name "many_to_one_mapping" inside mysql database.
com.mysql.cj.jdbc.Driver jdbc:mysql://localhost:3306/many_to_one_mapping root ######## org.hibernate.dialect.MySQL5Dialect true update
package com.hareeshsoni.blog.hibernate.mapping; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.hareeshsoni.blog.hibernate.mapping.model.Course; import com.hareeshsoni.blog.hibernate.mapping.model.Student; import com.hareeshsoni.blog.hibernate.mapping.util.HibernateUtil; public class MainApp { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); Student student1 = new Student("Mohan"); Student student2 = new Student("Sohan"); Student student3 = new Student("Ram"); Course course = new Course("Hibernate"); student1.setCourse(course); student2.setCourse(course); student3.setCourse(course); session.save(student1); session.save(student2); session.save(student3); session.getTransaction().commit(); session.close(); sessionFactory.close(); } }
Now run MainApp.java
Output
Database tables:
Student Table
Course Table