In a previous article (click here to visit it), we have discussed about how to map many to many relationship with @ManyToMany annotation. The @ManyToMany annotation is useful only in situations where we need to map only the many to many relationship between entities. It helps to generate a joined table with only the foreign keys that refer the participant tables.
But there are some situations where we need to add some extra columns to the joined table (newly generated intermediate table). In such case, we cannot use the @ManyToMany annotation for mapping many to many relationship between entities.
In this article, i am going to show you how to map many to many relationship and add extra column(s) to the joined table.
Please refer the following ER diagram.
You can see that the relationship has two attributes known as from_date and to_date. When we convert the above ER-Diagram into the relational table structure, we should get the following tables.
you can see that the attributes in the relationship has been added to the joined(intermediate) table which represents the relationship .
Lets implement this relationship with Spring Data JPA Project. The full source code of this article can be found at GitHub.
With @ManyToMany annotation, we just maintain only two entities. The intermediate(joined) table is generated as a result of mapping the many to many relationship between two entities.
But in this approach (adding extra columns) we need to maintain the relationship as a separate entity. So here we will be maintaining three separate entities.
Lets look at each class in detailed.
This is used to represent the many to many relationship between Student and Course entities. All the extra columns that need to be added for the joined table will be declared here.
As you can see that all the extra columns known as from_date and to_date has been declared here. In addition the relationship from StudentCourseEnrollment to Student and Course has been declared.
- Many StudentCourseEnrollment entries can refer a single entry in the Course. Therefore the relationship from StudentCourseEnrollment to Course is many-to-one
- Many StudentCourseEnrollment entries can refer a single entry in the Student. Therefore the relationship from StudentCourseEnrollment to Student is many-to-one
The joined table (intermediate table) will have a composite primary key which is composed with student_id and course_id. Since it is a composite key, it will be declared in a separate class and added with @IdClass annotation.
If you observe the StudentCourseId class properly, you may have noticed following points.
- It contains only the properties that are required to form the primary key (composite primary key) of the StudentCourseEnrollment entity.
- The property names of StudentCourseId class are exactly the same as the names in the related properties of the StudentCourseEnrollment class.
- It must implements Serializable interface.
Here we are not going to map the direct relationship from Student to Course. We have used an intermediate entity called StudentCourseEnrollment. The relationship from Student to StudentCourseEnrollment is one-to-many. One student can enroll for many number of courses. The same theory is applied to the relationship from Course to StudentCourseEnrollment too. mappedBy attributes is used to denote the property who owns the relationship.
Run and Test the source code.
You can get the source code related to this project from GitHub (Click here). Please change the database configuration in application.properties based on your development environment. The run the following command to build the project.
After executing the above command successfully, the table structure will be created with all declared properties.
Here i have shown you how to map many to many relationship with extra columns for the joined table. In here we have not used @ManyToMany annotation and we have used some alternative different approach.