Configuring many-to-many relationships

We will now configure Course and Student entities for a many-to-many relationship (a course can have many students, and one student can take many courses).

Many-to-many relations could be unidirectional or bidirectional. For example, you may only want to track students enrolled in the courses (so the Course entity will have a list of students) and not students taking the courses (the Student entity does not keep a list of courses). This is an unidirectional relationship where only the Course entity knows about the students, but the Student entity does not know about the courses).

In a bidirectional relationship, each entity knows about the other one. Therefore, the Course entity will keep a list of students and the Student entity will keep a list of courses. We will configure the bidirectional relationship in this example.

A many-to-many relationship also has one owning side and the other inverse side. You can mark either entity in the relationship as the owning entity. From the configuration point of view, the inverse side is marked by the mappedBy attribute to the @ManyToMany annotation.

In our application, we will make Student as the owning side of the relationship and Course as the inverse side. A many-to-many relationship in the database needs a join table, which is configured in the owning entity using the @JoinTable annotation.

We will first configure a many-to-many relationship in the Course entity. Add a member variable in Course to hold a list of Student entities and add the getter and the setter for it:

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

Then, click on the students field (added previously) and notice the settings in the JPA Details view:

Figure 4.27: Default JPA details for the students field in Course Entity

Because the students field is a list of Student entities, Eclipse has assumed a one-to-many relationship (see the link at the top of the JPA Details view). We need to change this. Click on the one_to_many link and select Many To Many.

Check the Merge and Refresh cascade options. Since we are putting a Course entity on the inverse side of the relationship, select Mapped By as Joining Strategy. Enter courses in the Attributes text field. The compiler will show an error for this because we don't have a courses field in the Student entity yet. We will fix this shortly. The JPA settings for the students field should be as shown in the following screenshot:

Figure 4.28: Modified JPA settings for the students field in Course Entity

Annotations for the students field in the Course entity should be as follows:

@ManyToMany(cascade = { MERGE, REFRESH }, mappedBy = "courses") 
private List<Student> students; 

Open Student.java in the editor. Add the courses field and the getter and the setter for it. Click on the courses field in the file and change the relationship from one-to-many to many-to-many in JPA Details view (as described previously for the students field in the Course entity). Select the Merge and Refresh cascade options. In the Joining Strategy section, make sure that the Join table option is selected. Eclipse creates the default join table by concatenating the owning table and the inverse table, separated by an underscore (in this case Student_Course). Change this to Course_Student to make it consistent with the schema that we created for the JDBC application.

In the Join columns section, select the Override default checkbox. Eclipse has named the join columns students_id->id, but in the Course_Student table we created in the JDBC application, we had a column named student_id. So, click the Edit button and change the name to student_id.

Similarly, change Inverse join columns from courses_id->id to course_id->id. After these changes, the JPA Details for the courses field should be as shown in the following screenshot:

Figure 4.29: JPA Details for the courses field in Student entity

The previous settings create the following annotations for the courses field:

@ManyToMany(cascade = { MERGE, REFRESH }) 
@JoinTable(name = "Course_Student", joinColumns = @JoinColumn(name = "student_id", referencedColumnName = "id"), inverseJoinColumns = 
@JoinColumn(name = "course_id", referencedColumnName = "id")) List<Course> courses;

We have set all the entity relationships required for our application. Download the accompanying code for this chapter to see the complete source code for Course, Student, and Teacher entities.

We need to add the entities we created previously in persistence.xml. Open the file and make sure that the General tab is open. In the Managed Classes session, click the Add button. Type the name of the entity you want to add (for example, Student) and select the class from the list. Add all the four entities we have created:

Figure 4.30: Add entities in persistence.xml
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.209.131