我有两个名为 Course 和 Student 的 Bean,它们具有如下所示的多对多关系。
类(class).java
package ca.sheridancollege.beans;
import lombok.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
@NamedQuery(name="Course.byProfID", query="from Course where prof_id=:id")
public class Course implements Serializable {
@Id @GeneratedValue
private int id;
private String courseName;
private String department;
@ManyToOne(cascade=CascadeType.ALL)
private Professor prof;
@ManyToMany(cascade=CascadeType.ALL, mappedBy="courseList")
private List<Student> studentList = new ArrayList<Student>();
public Course(String cName, String dept) {
super();
this.courseName = cName;
this.department = dept;
}
}
学生.Java
package ca.sheridancollege.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import lombok.*;
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@NamedQuery(name="Student.byID", query="from Student where id=:id")
public class Student implements Serializable{
@Id @GeneratedValue
private int id;
private String firstName;
private String lastName;
private String email;
@ManyToMany(cascade=CascadeType.ALL)
private List<Course> courseList = new ArrayList<Course>();
public Student(String fName,String lName, String e) {
this.firstName = fName;
this.lastName = lName;
this.email = e;
}
}
我必须为学生分配一些类(class),这是我通过这种 dao 方法完成的。
public void assignCourse(int cid, int sid) {
Session session = sessionFactory.openSession();
session.beginTransaction();
Student s = (Student) session.get(Student.class, sid);
Course c = (Course) session.get(Course.class, cid);
s.getCourseList().add(c);
c.getStudentList().add(s);
session.getTransaction().commit();
session.close();
}
此方法会生成一个包含正确数据的连接表,称为 Student_course。 StudentList_id = 学生的 ID,courseList_id = 我分配的类(class) ID。
select * from student_course;
+----------------+---------------+
| studentList_id | courseList_id |
+----------------+---------------+
| 5 | 3 |
| 6 | 4 |
+----------------+---------------+
select * from course;
+----+------------+------------+---------+
| id | courseName | department | prof_id |
+----+------------+------------+---------+
| 3 | Java101 | Technology | 1 |
| 4 | Biology | Science | 2 |
+----+------------+------------+---------+
select * from student;
+----+-----------------+-----------+----------+
| id | email | firstName | lastName |
+----+-----------------+-----------+----------+
| 5 | sabeeh@mail.com | Sabeeh | Shah |
| 6 | td@mail.com | test | student |
+----+-----------------+-----------+----------+
现在我试图从数据库中获取每个学生拥有的类(class)并将其显示在jsp中,但我不知道如何从连接表中进行选择,我认为这甚至是不允许的,那么其他方式是什么我能实现这个吗?
最佳答案
要创建您想要的连接表“student_course”,您需要在关系的一侧使用@JoinTable,您不应该使用“mappedBy”,您正在做的是创建两个单独的关系
@ManyToMany(cascade=CascadeType.ALL)
@JoinTable
private List<Student> studentList = new ArrayList<Student>();
关于Java多对多从连接关系中检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47622450/