Hibernate @Many到很多分组的地图映射

时间:2016-03-25 05:06:49

标签: java hibernate

我想解决这个问题,因为它看起来很有趣,直接从盒子里获取分组对象 关于这个,我有db sheme:

Teacher
-id
-name

Group
-id
-name

Subject
-id
-name

Several teachers can teaches one subject

Subject_teachers
-subject_id
-teacher_id

Group_subjects
-group_id
-subject_id

我会按教师小组分组课程

class Teacher{
 @Id id ,
...
   @ManyToMany
   ???
   Map<Group,Subject) subjects;
};

1 个答案:

答案 0 :(得分:0)

      @Entity
      @Table(name = "Teacher")
      public class Teacher {

            private Integer teacherId;
            private String name;
            private Set<SubjectTeachers> subjectTeachers = new HashSet<SubjectTeachers>(0);

            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name = "teacher_id", unique = true, nullable = false)
            public Integer getTeacherId() {
                return teacherId;
            }

            public void setTeacherId(Integer teacherId) {
                this.teacherId = teacherId;
            }

            @Column(name = "name", nullable = false)
            public String getName() {
                return name;
            }   

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

            @OneToMany(mappedBy = "subjectTeachers", cascade = CascadeType.ALL, fetch=FetchType.LAZY)
            public Set<SubjectTeachers> getSubjectTeachers() {
                return subjectTeachers;
            }

            public void setProjectTags(Set<SubjectTeachers> subjectTeachers) {
                this.subjectTeachers = subjectTeachers;
            }
        }

    @Entity
    @Table(name = "Subject")
    public class Subject{

        private Integer subjectId;
        private String name;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "subject_id", unique = true, nullable = false)
        public Integer getSubjectId() {
            return subjectId;
        }

        public void setSubjectId(Integer subjectId) {
            this.subjectId = subjectId;
        }

        @Column(name = "name", nullable = false)
        public String getName() {
            return name;
        }   

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

    }

    @Entity
    @Table(name ="subject_teachers")
    public SubjectTeachers{

        private Integer subjectTeachersID;
        private Teacher teacher;
        private Subject subject;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "subjectTeachersID", unique = true, nullable = false)
        public Integer getSubjectTeachersId() {
            return subjectTeachersID;
        }

        public void setSubjectTeachersId(Integer subjectTeachersID) {
            this.subjectTeachersID = subjectTeachersID;
        }


        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinColumn(name = "teacher_id", nullable = false)
        public Teacher getTeacher() {
            return teacher;
        }

        public void setTeacher(Teacher teacher) {
            this.teacher = teacher;
        }


        @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinColumn(name = "subject_id", nullable = false)
        public Subject getSubject() {
            return subject;
        }

        public void setSubject(Subject subject) {
            this.subject = subject;
        }   

    }

    @Entity 
    @Table(name = "group_subjects")
    public class GroupSubjects{

        private Integer groupId;

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "group_id", unique = true, nullable = false)
        public Integer getGroupId() {
            return groupId;
        }

        public void setGroupId(Integer groupId) {
            this.groupId = groupId;
        }

        private Subject subject;

        @ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
        @JoinColumn(name = "subject_id", nullable = false)
        public Subject getSubject() {
            return subject;
        }

        public void setSubject(Subject subject) {
            this.subject = subject;
        }   
    }

希望对你有所帮助..