Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:59:26+00:00 2026-05-25T22:59:26+00:00

I have a USER table and a COURSE table. A USER can have many

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:59:26+00:00Added an answer on May 25, 2026 at 10:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user table 'users' that has fields like: id first_name last_name ...
I have a user table in my mysql database that has a password column.
I have a User table that has all of their avatars saved in an
I have two tables of concern here: users and race_weeks. User has many race_weeks,
We have a user table, every user has an unique email and username. We
I have a User table which has a PrivilegeId foreign key points to a
I have an user defined table function in SQL Server that aggregate data from
I have a user table in postgress. Each user can have from 0 to
There are many users, many groups that users can be part of. Each group
I have a MySQL table containing userID , score . A single user can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.