使用JOIN-Table休眠多对多关联

时间:2018-10-01 12:44:05

标签: java mysql hibernate associations hibernate-mapping

我发现使用联接表难以理解休眠的多对多关联。

我从net获得示例项目。 它包括

Course.java

package com.chad.model;

import java.util.List;

import javax.persistence.CascadeType;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="chad_course")
public class Course {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="title")
    private String title;

    @ManyToMany(fetch=FetchType.LAZY,cascade={CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST})
    @JoinTable(name="course_student",
               joinColumns=@JoinColumn(name="course_id"),
               inverseJoinColumns=@JoinColumn(name="student_id"))
    private List<Student> studentList;

    public Course()
    {

    }

    public Course( String title) {
        this.title = title; 
    }




    public int getId() {
        return id;
    }


    public void setId(int id) {
        this.id = id;
    }


    public String getTitle() {
        return title;
    }


    public void setTitle(String title) {
        this.title = title;
    }

    public List<Student> getStudentList() {
        return studentList;
    }

    public void setStudentList(List<Student> studentList) {
        this.studentList = studentList;
    }


}

Student.java

package com.chad.model;

import java.util.List;

import javax.persistence.CascadeType;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name="Chad_student")
public class Student {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;

    @Column(name="FIRST_NAME")
    private String firstName;

    @Column(name="LAST_NAME")
    private String lastName;

    @Column(name="EMAIL_ID")
    private String email;

    @ManyToMany(fetch=FetchType.LAZY,cascade={CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST})
    @JoinTable(name="course_student",
                joinColumns=@JoinColumn(name="student_id"),
                inverseJoinColumns=@JoinColumn(name="course_id"))
    private List<Course> courseList;

    public Student(){}

    public Student(String firstName, String lastName, String email) {
        super();
        this.firstName = firstName;
        this.lastName = lastName;
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


}
  

1)我的疑问是,我从互联网上获得的例子有

@ManyToMany(fetch=FetchType.LAZY,cascade={CascadeType.REFRESH,CascadeType.MERGE,CascadeType.PERSIST})
        @JoinTable(name="course_student",
                    joinColumns=@JoinColumn(name="student_id"),
                    inverseJoinColumns=@JoinColumn(name="course_id"))

在两个实体类中编写的上述代码块。 但是他们在其他一些文章中写道 一个实体类中的同一块。
而在另一个实体类中,只有@ManyToMany->没有joinColumn

我能知道这两种语法的重要性吗?

  

2)运行该程序后,我得到了以下日志

Main.java

Course course = new Course("Photography");

        session.save(course);

        Student student1 = new Student("John", "Dao", "john@gmail.com");
        Student student2 = new Student("John1", "Dao1", "john1@gmail1.com");


        List<Student> studentList = new ArrayList<Student>();
        studentList.add(student1);
        studentList.add(student2);

        course.setStudentList(studentList);

        session.save(student1);
        session.save(student2);

        session.getTransaction().commit();

日志

Hibernate: insert into chad_course (title, instructor_id) values (?, ?)
Hibernate: insert into Chad_student (FIRST_NAME, LAST_NAME, EMAIL_ID) values (?, ?, ?)
Hibernate: insert into Chad_student (FIRST_NAME, LAST_NAME, EMAIL_ID) values (?, ?, ?)
Hibernate: insert into course_student (course_id, student_id) values (?, ?)
Hibernate: insert into course_student (course_id, student_id) values (?, ?)

现在

  • 第一次插入到chad_course中。 正确

  • 第二和第三插入插入Chad_student。 正确

  • 第4和第5个插入项插入了course_student->休眠如何知道 要插入的course_id和student_id,因为没有选择 查询正在发生。

1 个答案:

答案 0 :(得分:0)

GenerationType.IDENTITY用于支持自动增量的数据库(例如MySQL,SQLServer和其他少数几个数据库),但并非在所有数据库(例如Oracle等)中都可用。

这是您看不到任何选择SQL查询被触发的原因

检查-GenerationType.AUTO vs GenerationType.IDENTITY in hibernate