Apr
15
2015

Example One to many and many to one mapping and mappedBy

One to many relationship

In this post we will see about one to many . In this  scenario imagine that one student can opt more than one course. Another example we can take student and instructor relationship . One instructor can advise many student. Another example that we will see in this post is course and department. One course can have multiple department. There are many real world scenario of this kind of relationship.

many to one relatioship dept and course

many to one relatioship dept and course

CourseType.java

Course.java

@OneToMany relation annotation tells hibernate that course is collection of entity and it need to save in different table.

Department.java

hibernate.cfg.xml

OneToManyMapping.java

Run this program .Here is query that hibernate run.

Hibernate: insert into Course (courseFee, courseName, type) values (?, ?, ?)
Hibernate: insert into Department (dept_name, head_name) values (?, ?)
Hibernate: insert into Department (dept_name, head_name) values (?, ?)
Hibernate: insert into Course_Department (Course_courseId, department_dept_id) values (?, ?)
Hibernate: insert into Course_Department (Course_courseId, department_dept_id) values (?, ?)

Here you can see hibernate has created a mapping table with name Course_Department and has inserted course id and department id mapped in this table. The mapping table name  is default given by hibernate is combination of both classes department and course.  Also column of this mapping table are combination of department class and primary key , course class and its primary key. We can change these these default name configuration.

@JoinTable(name=”department_course_mapping”,joinColumns=@JoinColumn(name=”course”),inverseJoinColumns=@JoinColumn(name=”department”))

@joinTable annotation name attribute tell hibernate name of mapping table. Joincolumns and inverse Joincolumns tells name of column of this mapping table. Joincolumns specify name of column for course id and inverseJoincolumns specify name of column for department id. After adding @JoinTable see what query hibernate make to insert data.

  Hibernate: insert into Course (courseFee, courseName, type) values (?, ?, ?)
Hibernate: insert into Department (dept_name, head_name) values (?, ?)
Hibernate: insert into Department (dept_name, head_name) values (?, ?)
Hibernate: insert into department_course_mapping (course, department) values (?, ?)
Hibernate: insert into department_course_mapping (course, department) values (?, ?)

Many to one relationship and mappedBy


Well this was one to many relation . There can also exist many to one relationship. For example many student can be instructed by single instructor, Many department can be there in a course.

Course.java

annotation @OneToMany(mappedBy=”course”) establish one to many relationship between department and course. Here mappedBy tell hibernate that mapping for this relationship is already done in department table with course object. 

Department.java

@ManyToOne annotation is used for many to one relationship. In Student class we already have many to one mapped.

@JoinColumn(name=”courseId”) tell hibernate what should be name of column for course  id.

ManyToOneMapping.java

When we run program following queries executed

Hibernate: insert into Course (courseFee, courseName, type) values (?, ?, ?)
Hibernate: insert into Department (courseId, dept_name, head_name) values (?, ?, ?)
Hibernate: insert into Department (courseId, dept_name, head_name) values (?, ?, ?)

There only two table is created. As you can see there is no column for course column in course because one course can have multiple courses but department table have course column because each department have only one student associated with it.

This is all in this post of hibernate tutorial . In next tutorial we will see many to many relationship.

About the Author: devender kumar

java/j2EE developer

Comments are closed.