简体   繁体   中英

How can I create an intermediate table between to entities using @ManyToMany with hibernate?

I did this a few days ago, I had an intermediate table like this:

-------------------------
|COURSE_ID | STUDENT_ID |
------------------------
|0         |1           |
|0         |2           |
|1         |1           |
|1         |2           |
-------------------------

but then I wanted to add fields to it (and didn't commit it to github ¬¬, I was in a hurry) and ended up breaking the whole thing up... Long story short I started this example again. My code now creates the two tables "courses" and "students" but they don't reflect anything about the relationship between them, I mean not intermediate table is created; though I can ask for the courses to every student and vice versa in the object world, in the databases world none of this is reflected. Here is my code:


Student class package model;

import java.util.HashSet;
import javax.persistence.JoinColumn;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import java.util.Set;

@Entity
@Table(name="STUDENTS")
public class Student
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id ;

    private String name ;

    @ManyToMany(targetEntity=Course.class, mappedBy="courseName", fetch=FetchType.EAGER)
    private Set<Course> courses ;

    public Student(String myName)
    {
        this.name = myName ;
        courses = new HashSet<Course>() ;
    }

    public void addCourse(Course myCourse)
    {
        this.courses.add(myCourse) ;
    }


    @Column(name="STUDENT_ID")
    public int getStudent_id()
    {
        return this.id ;
    }


    @JoinTable(name="COURSE_STUDENTS",
        joinColumns=
            @JoinColumn(name="STUDENT_ID"),
        inverseJoinColumns=
            @JoinColumn(name="COURSE_ID")
                )
    public Set<Course> getCourses()
    {
        return this.courses ;
    }

    @Column(name="STUDENT_NAME")
    public String getStudent_name()
    {
        return this.name ;
    }

}

Course class

package model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="COURSES")
public class Course
{
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int id ;

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

    @ManyToMany(targetEntity=Student.class, mappedBy="name", fetch=FetchType.EAGER)
    private Set<Student> students ;


    public Course(String myName)
    {
        this.courseName = myName ;
        this.students = new HashSet<Student> () ;
    }

    public void addStudent(Student myStudent)
    {
        this.students.add(myStudent) ;
    }

    @ManyToMany(mappedBy="courses")
    public Set<Student> getCourse_students()
    {
        return this.students ;
    }

    @Column(name="COURSE_ID")
    public int getCourse_id()
    {
        return id ;
    }
}

Main class that tests this

package controller;

import javax.persistence.EntityManager;

import db.EntityManagerHelper;
import model.Student;
import model.Course;

public class Main {

    public static void main(String[] args) {

        EntityManager entityManager= EntityManagerHelper.getEntityManager();

        Course course_kK = new Course("K1000") ;
        Course course_mK = new Course("M1000") ;
        Course course_rK = new Course("R1000") ;

        Student student_rick = new Student("Rick") ;
        student_rick.addCourse(course_rK);
        student_rick.addCourse(course_mK);

        Student student_rock = new Student("Rock") ;
        student_rock.addCourse(course_rK);
        student_rock.addCourse(course_kK);

        Student student_jack = new Student("Jack") ;
        student_jack.addCourse(course_mK);
        student_jack.addCourse(course_kK);

        course_kK.addStudent(student_rock);
        course_kK.addStudent(student_jack);
        course_mK.addStudent(student_jack);
        course_mK.addStudent(student_rick);
        course_rK.addStudent(student_rock);
        course_rK.addStudent(student_rick);

        System.out.println("course_rK:id = "+course_rK.getCourse_id());
        System.out.println("course_kK:id = "+course_kK.getCourse_id());
        System.out.println("course_mK:id = "+course_mK.getCourse_id());

        EntityManagerHelper.beginTransaction();

        entityManager.persist(course_rK);
        entityManager.persist(course_kK);
        entityManager.persist(course_mK);

        entityManager.persist(student_rick);
        entityManager.persist(student_rock);
        entityManager.persist(student_jack);

        entityManager.flush();
        EntityManagerHelper.commit();   

        System.out.println("student_jack:id = "+student_jack.getStudent_id());
        System.out.println("student_rock:id = "+student_rock.getStudent_id());
        System.out.println("student_gast:id = "+student_rick.getStudent_id());
    }
}

How can I create that intermediate table between those two entities that "brakes" the many-to-many relationship?

Here's the detailed answer. I had to make changes in a few lines... I checked out this tutorial (again), the same I first followed when I was able to do what I asked in my original question.

where it said

@Column(name="COURSE_ID")

I changed that to

@Column(name="COURSE_ID", unique=true, nullable=false)

where it said

@Column(name="STUDENT_ID")

I changed that to

@Column(name="STUDENT_ID", unique=true, nullable=false)

where it said

@ManyToMany(targetEntity=Course.class, mappedBy="courseName", fetch=FetchType.EAGER)

I changed that to

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)

where it said

@ManyToMany(mappedBy="courses")

I changed that to

@ManyToMany(fetch = FetchType.LAZY, mappedBy = "courses")

And where it said

 @JoinTable(name="COURSE_STUDENTS",
        joinColumns=
            @JoinColumn(name="STUDENT_ID"),
        inverseJoinColumns=
            @JoinColumn(name="COURSE_ID")
                )
    public Set<Course> getCourses()
    {
        return this.courses ;
    }

I changed that to

@JoinTable(name = "STUDENT_COURSE", joinColumns = {
        @JoinColumn(name = "STUDENT_ID", nullable = false, updatable = false) },
        inverseJoinColumns = { @JoinColumn(name = "COURSE_ID",
                nullable = false, updatable = false) }
            )
public Set<Course> getCourses()
{
    return this.courses ;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM