父集的JPA / Hibernate子集

时间:2015-04-17 08:52:34

标签: java hibernate jpa

让我们假设我们有以下实体关联:

    public class Classroom {


        @OneToMany(mappedBy="classroom")
        private Set<Session> sessions;

        @OneToMany(mappedBy="classroom")
        private Set<Instructor> instructors;

        @OneToMany(mappedBy="classroom")
        private Set<Student> students;
    }


    public class Session {

        @ManyToOne
        private Classroom classroom;

        @ManyToMany
        private Set<Instructors> instructors;

        @ManyToMany
        private Set<Student> students;

    }

    public class Student {

        @ManyToOne
        private Classroom classroom;

        @ManyToMany(mappedBy="students")
        private Set<Session> sessions;
    }

    public class Instructor {

        @ManyToOne
        private Classroom classroom;

        @ManyToMany(mappedBy="instructors")
        private Set<Session> sessions;

    }

一般的想法是Sessions属于特定的课堂,属于该课程的学生+教师应该是属于课堂的学生和教师。

我可以在服务层处理这个问题,但有没有更好的方法直接使用JPA处理这个问题?

0 个答案:

没有答案