如何在没有额外类的情况下引用JPA中的ManyToMany表?

时间:2019-06-02 16:53:40

标签: java hibernate jpa

说我有以下课程:

课程:

@Entity
public class Course {

    private @Id @GeneratedValue Long id;

    @ManyToMany
    @JoinTable(
    name = "course_subject", 
    joinColumns = @JoinColumn(name = "courseId"), 
    inverseJoinColumns = @JoinColumn(name = "subjectId"))
    Set<Subject> subjects;
}

主题课程

@Entity
public class Subject {

    private @Id @GeneratedValue Long id;

    @ManyToMany(mappedBy = "subjects")
    Set<Course> courses;
}

这将在数据库上创建正确的架构。但是,如果我想给生成的course_subject表一个id字段,然后在另一个类中引用该表怎么办?像这样:

@Entity
public class Class {

    private @Id @GeneratedValue Long id;

    // How to reference the generated table?
    private Long courseSubjectId;
}

有什么方法可以通过JPA进行操作而无需创建额外的Java类?

0 个答案:

没有答案