Friday, February 17, 2012

Many to Many mapping in JPA annotations

ER diagram
------





Student.java
package org.convey.user.registration.model;

import javax.persistence.*;
import java.util.List;

/**
 * Name: Chathuranga Tennakoon
 * Mobile: 0094759610139
 * Blog: chathurangat.blogspot.com
 * Email: chathuranga.t@gmail.com
 */
@Entity
@Table(name="student")
public class Student {

    @Id
    @GeneratedValue
    @Column(name = "student_id")
    private int studentId;

    @Column(name="student_name")
    private String studentName;

    @Column(name="student_address")
    private String studentAddress;

    @Column(name="version")
    private long version;

    @ManyToMany
    @JoinTable(
            name="student_course",
            joinColumns = {@JoinColumn(name = "studentID",referencedColumnName = "student_id")},
            inverseJoinColumns = {@JoinColumn(name = "courseID", referencedColumnName = "course_id")})
    private List courses;


    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 String getStudentAddress() {
        return studentAddress;
    }

    public void setStudentAddress(String studentAddress) {
        this.studentAddress = studentAddress;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public List getCourses() {
        return courses;
    }

    public void setCourses(List courses) {
        this.courses = courses;
    }
}



Course.java

package org.convey.user.registration.model;

import javax.persistence.*;
import java.util.List;

/**
 * Name: Chathuranga Tennakoon
 * Mobile: 0094759610139
 * Blog: chathurangat.blogspot.com
 * Email: chathuranga.t@gmail.com
 */
@Entity
@Table(name="course")
public class Course {

    @Id
    @GeneratedValue
    @Column(name="course_id")
    private int courseId;

    @Column(name="course_name")
    private String courseName;


    @Column(name="course_description")
    private String courseDescription;

    @Column(name="version")
    private long version;

    @ManyToMany
    @JoinTable(
            name="student_course",
            joinColumns = {@JoinColumn(name = "courseID",referencedColumnName = "course_id")},
            inverseJoinColumns = {@JoinColumn(name = "studentID", referencedColumnName = "student_id")})
    private List<Student>  students;


    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;
    }

    public String getCourseDescription() {
        return courseDescription;
    }

    public void setCourseDescription(String courseDescription) {
        this.courseDescription = courseDescription;
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public List<Student> getStudents() {
        return students;
    }

    public void setStudents(List<Student> students) {
        this.students = students;
    }

}//course


  • Student can register for many courses.
  • A given course has many registered students
therefore many to many relationship exists between these two entities.therefore @ManyToMany Annotation is used to map the relationship between these two entities.

@JoinTable annotation is declared in the POJO class that owns the relationship. since this is many to many relationship, both class own the relationship. therefore @JoinTable annoatation is declared in the both classes. (therefore referential integrity constraint will be accurately maintained)

hope this will helpful for you!

 

No comments:

Post a Comment