将联结表中的值映射到实体 [英] Mapping value in junction table to Entity

查看:71
本文介绍了将联结表中的值映射到实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个USER表和一个COURSE表.一个用户可以有多个课程,一个用户可以有多个课程.联结表包含一个ROLE值,该值确定用户在课程中所扮演的角色(即讲师,学生等).我需要一些如何将此角色与每个用户的课程相关联.

I have a USER table and a COURSE table. A USER can have many COURSES, and a COURSE many USERS. The junction table contains a ROLE value that determines what role the user has in a COURSE (i.e. Instructor, Student, etc.). I need to some how associate this role with the COURSE for each USER.

如果我将角色放置在Course类中,则该角色将不起作用,因为Course具有许多用户,反之亦然.在User类中

If I put the role in the Course class, it can't work, since a Course has many users, and vice versa in the User class.

这是我到目前为止所拥有的:

Here's what I have so far:

@Entity
@Table(name = "USERS")
public class User {

@Id
@Column(name = "PK1")
private Long id;

@Column(name = "USER_ID")
private String userId;

@ManyToMany
@JoinTable(name = "COURSE_USERS", 
    joinColumns = @JoinColumn(name = "USERS_PK1", referencedColumnName = "PK1"), 
    inverseJoinColumns = @JoinColumn(name = "CRSMAIN_PK1", referencedColumnName = "PK1"))
private Collection<Course> courses;

...

@Entity
@Table(name = "COURSE")
@SecondaryTable(name = "COURSE_USERS", 
pkJoinColumns = @PrimaryKeyJoinColumn(name = "CRSMAIN_PK1"))
public class Course {

@Id
@Column(name = "PK1")
private Long id;

    // THIS PROBABLY WON'T WORK //
@Column(name = "ROLE", table = "COURSE_USERS")
private Character role;

@Column(name = "AVAILABLE_IND")
private boolean available;

@Column(name = "COURSE_NAME")
private String name;

@Transient
private String url;

    ...

注意:我无法更改数据库架构,因此联结表是不可协商的.

Note: I cannot change the database schema, so the junction table is nonnegotiable.

推荐答案

您的情况下有第三个实体,它想出来.您可以将其称为CourseAssignment. CourseAssignment将包含角色以及与用户和角色的ManyToOne关系.此外,当然还有从课程到课程分配以及从用户到课程分配的一对多关系.

There is third entity in your case, and it wants to come out. You can call it CourseAssignment. CourseAssignment will contain role, and ManyToOne relationship to both User and Role. And additionally of course OneToMany relationship from Course to CourseAssignment and from User to CourseAssignment.

类似的事情会起作用(我没有尝试过,所以可能会丢失一些东西,但是您会有所想法的.

Something like this will work (I didn't tried it, so something can be missing, but you will get idea.

@Entity
@Table(name = "COURSE_USERS")
@IdClass(CourseAssignmentId.class)
public class CourseAssignment {
    @Id
    @ManyToOne
    @JoinColumn(name="USERS_PK1")
    private User user;
    @Id
    @ManyToOne
    @JoinColumn(name="CRSMAIN_PK1")
    private Course course;

    @Column(name = "ROLE")
    private Character role;
}
//and then of course IdClass, because combined key:
@Embeddable
public class CourseAssignmentId implements Serializable{
    @Column(name="USERS_PK1")
    private Long user;

    @Column(name="CRSMAIN_PK1")
    private Long course;
}


User { ..

    @OneToMany(mappedBy = "user")
    private Collection<CourseAssignment> courseAssignments;
...
}

Course {..
   @OneToMany(mappedBy = "course")
   private Collection<CourseAssignment> course;

..
}

当然要删除那些现有的关系连接属性.

And of course remove those existing relationship connected attributes.

这篇关于将联结表中的值映射到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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