Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
542 views
in Technique[技术] by (71.8m points)

jpa - Mapping value in junction table to Entity

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...