Java多对多从连接关系中检索数据

时间:2017-12-03 19:38:28

标签: java mysql hibernate java-ee

我有两个名为Course and Student的bean,它们具有多对多关系,如下所示。

Course.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;
    }


}

Student.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;

    }


}

我必须通过这个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 =我分配的课程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  |
+----+-----------------+-----------+----------+

现在我想从数据库中获取每个学生所拥有的课程并将其显示在一个jsp中,但我不知道如何从一个我认为甚至不允许的联合表中进行选择那么什么是其他方式我可以做到这一点?

1 个答案:

答案 0 :(得分:0)

创建你想要的联结表&#34; student_course&#34;你需要在关系中使用@JoinTable,你不应该使用&#34; mappedBy&#34;你正在做的是创建两个单独的关系

@ManyToMany(cascade=CascadeType.ALL)
@JoinTable
private List<Student> studentList = new ArrayList<Student>();