JPA ManyToMany,JoinTable怎么会有属性呢?

发布于 2024-12-22 13:41:00 字数 1461 浏览 5 评论 0原文

我有一个关于EJB中ManyToMany设计的问题,jointable如何拥有属性?
举个例子,学生和课程都是ManyToMany,每个学生有很多门课程,很多学生选择一门课程。

    @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 中有一个属性,例如每个学生的一门课程都有一个分数。如何在 EJB 中使用 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;
        }
    }

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-12-29 13:41:00

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

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.

叹倦 2024-12-29 13:41:00

这是可能的。

您只需通过第三个实体将多对多映射替换为一对多和多对一映射的显式组合,这将表示两个主要实体(示例中的学生和课程)之间的关联)。

请在此处阅读详细信息

It is possible.

You just need to replace many-to-many mapping with the explicit combination of one-to-many and many-to-one mappings via a 3rd entity, that would represent the association between the two primary entities (student and course in your example).

Please read details here

秉烛思 2024-12-29 13:41:00

具有多对多关系的实体(商家和服务)。
这可以使用第三方实体来实现,如下所示:-

@Entity
@Table(name = "merchant")
public class Merchant implements java.io.Serializable {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.merchant",targetEntity = MerchantService.class)
    private Set<MerchantService> merchantServices = new HashSet<>();
}

@Entity
@Table(name = "merchant_service")
@AssociationOverrides({
        @AssociationOverride(name = "pk.merchant",
            joinColumns = @JoinColumn(name = "merchant_id")),
        @AssociationOverride(name = "pk.service",
            joinColumns = @JoinColumn(name = "service_id")) })
public class MerchantService implements java.io.Serializable {

    @EmbeddedId
    private MerchantServiceId pk = new MerchantServiceId();

    private boolean isActive;

    public MerchantServiceId getPk() {
        return pk;
    }

    public void setPk(MerchantServiceId pk) {
        this.pk = pk;
    }

    @Transient
    public Service getService() {
        return getPk().getService();
    }


    @Transient
    public Merchant getMerchant() {
        return getPk().getMerchant();
    }


    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

}

@Embeddable
public class MerchantServiceId implements java.io.Serializable {

    private Merchant merchant;
    private Service service;

    @ManyToOne
    public Merchant getMerchant() {
        return merchant;
    }

    @ManyToOne
    public Service getService() {
        return service;
    }

}

@Entity
@Table(name = "service")
public class Service implements java.io.Serializable {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.service",targetEntity = MerchantService.class)
    private Set<MerchantService> merchantServices = new HashSet<>();

}

Entities with Many to Many Relationships (Merchant and Service).
This can be achieved using a third entity as follow:-

@Entity
@Table(name = "merchant")
public class Merchant implements java.io.Serializable {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.merchant",targetEntity = MerchantService.class)
    private Set<MerchantService> merchantServices = new HashSet<>();
}

@Entity
@Table(name = "merchant_service")
@AssociationOverrides({
        @AssociationOverride(name = "pk.merchant",
            joinColumns = @JoinColumn(name = "merchant_id")),
        @AssociationOverride(name = "pk.service",
            joinColumns = @JoinColumn(name = "service_id")) })
public class MerchantService implements java.io.Serializable {

    @EmbeddedId
    private MerchantServiceId pk = new MerchantServiceId();

    private boolean isActive;

    public MerchantServiceId getPk() {
        return pk;
    }

    public void setPk(MerchantServiceId pk) {
        this.pk = pk;
    }

    @Transient
    public Service getService() {
        return getPk().getService();
    }


    @Transient
    public Merchant getMerchant() {
        return getPk().getMerchant();
    }


    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

}

@Embeddable
public class MerchantServiceId implements java.io.Serializable {

    private Merchant merchant;
    private Service service;

    @ManyToOne
    public Merchant getMerchant() {
        return merchant;
    }

    @ManyToOne
    public Service getService() {
        return service;
    }

}

@Entity
@Table(name = "service")
public class Service implements java.io.Serializable {

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.service",targetEntity = MerchantService.class)
    private Set<MerchantService> merchantServices = new HashSet<>();

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文