JPA ManyToMany,JoinTable怎么可以有一个属性? [英] JPA ManyToMany, how can JoinTable have a property?

查看:587
本文介绍了JPA ManyToMany,JoinTable怎么可以有一个属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个设计EJB中的ManyToMany的问题,可以如何联合一个属性?

这是一个例子,学生和课程是ManyToMany,每个学生都有很多课程,许多学生选择一门课程

I have a question for designing the ManyToMany in EJB, how can a jointable has a property?
Here is an example, the students and courses are ManyToMany, every student have many courses, and many students choose one course.

    @Entity
    public class Student implements Serializable { 
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        Long id;
        String name;
        private Collection<Course> courses; 

        @ManyToMany(mappedBy = "students",cascade=CascadeType.ALL)      
        public Collection<Course> getCourses() {
            return this.courses;
        }

        public void setCourses(Collection<Course> courses) {
            this.courses = courses;
        }

    }


    @Entity
    public class Course implements Serializable { 
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        Long id;
        String name; 
        private Collection<Student> students; 

        @ManyToMany(cascade=CascadeType.ALL)
        @JoinTable(name = "Student_Course",
        joinColumns = {@JoinColumn(name = "Course_ID", referencedColumnName = "id")}, 
        inverseJoinColumns = {@JoinColumn(name = "Student_ID", referencedColumnName = "id")})  

        public Collection<Student> getStudents() {
            return this.students;
        }

        public void setStudents(Collection<Student> students) {
            this.students = students;
        }
    }

但是如果我在JoinTable中有一个属性,例如,每个学生都有一门课程的成绩。


非常感谢您的关注!

However if I have a property in the JoinTable, for example each student has one score for one course. How can I make it in EJB with ManyToMany?
Many thanks for your attention!

推荐答案

这是不可能的,你不能添加属性关系。如果您需要访问连接表中的属性,那么该属性属于某个实体,因此您需要第三个实体。

It is not possible, you cannot add property to relationship. If you need to access property in the join table, then that property belongs to some entity and as a result you need third entity.

这篇关于JPA ManyToMany,JoinTable怎么可以有一个属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆