我发现很难理解使用联接表休眠多对多关联。
我从网上得到了一个样本项目。它包括
课程.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;
}
}
学生.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) 运行程序后,我得到了以下日志
主.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\u课程中。对的
第二个和第三个插页插入chad\u学生。对的
第4个和第5个insert是在course\u student-->由于没有发生select查询,hibernate如何知道要插入的course\u id和student\u id。
1条答案
按热度按时间smdncfj31#
generationtype.identity与支持自动增量的数据库(如mysql、sqlserver和其他一些数据库)一起使用,这在所有数据库(如oracle等)中都不可用。
这就是您没有看到任何selectsql查询被激发的原因
检查-generationtype.auto与hibernate中的generationtype.identity