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 8691783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:11:35+00:00 2026-06-13T00:11:35+00:00

I currently have a table which looks like this: Users username address dob …

  • 0

I currently have a table which looks like this:

Users

username
address
dob
…

And

Roles
username
role

I want to make a foreign key constraint between Role.username and User.username. How should I go about doing this with ann

  • 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-06-13T00:11:37+00:00Added an answer on June 13, 2026 at 12:11 am
    1. Would be good if you could drop the “s” from the end of your table/type names – each row/instance represents one item, not many.

    2. Use @ManyToOne annotated field within Role entity – with field of type “User” and name “user”. This is a relationship field that is owned by Role – the FK column will go in Role table.

    3. Optionally, use @OneToMany(mappedBy="user") annotation field within User entity – with field of type Collection<Role> or List<Role>, dependending on whether you want to preserve the ordering of app insertion when you write to DB. This is a non-owned relationship field, meaning no FK column will go into User table – we must use mappedBy to name the owned relationship on the User entity. However, if you omit (2) and include (3) this becomes owned, and then mappedBy is not used, but column=”some_db_colname” could be used instead.

    4. In User entity set @Id on username (single PK field of basic type).

    5. In Role entity set @Id on both user (non-basic type) and also role (basic type).

    Additionally, because you have two @Id fields and because one of them is not a basic type, you must additionally create a RoleId class that repeats the same field names, but this time, maps the data types to the underlying basic types – as found in the PK field of the referenced entities and as consistent with underlying DB table. Annotate Role with @IdClass(RoleId)

    @Entity
    public class User {
    
         @Id
         String username;  // for simplicity match the db column name
    
         String address;
    
         Date dateOfBirth;
    
         @OneToMany(mappedBy="user")
         Collection<Role> rolesForUser;
    
         // other fields and methods
    }
    
    @Entity
    @UserId(RoleId)
    public class Role {
    
         @Id
         @ManyToOne
         User user;
    
         @Id
         String role;  // for simplicity, match the DB column name
    
         // other fields and methods
    }
    
    public class RoleId {
    
         String user;
    
         String role;
    
         public RoleId() { // ...}  // all entities must have no-arg constructor
         // additional arg constructor - so we can set fields without having setters
         public RoleId(String user, String role) { this.user = user; this.role = role}  
    
         public String getUser() { return user; }  // omit setter: Id is immutable once created
         public String getRole() { return role; }  // omit setter: Id is immutable once created
    }
    

    Ready to use… =:-)

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

Sidebar

Related Questions

I have a table which is currently displayed like this: Question OptionType What is
I currently have a table structure that looks something like this(some details omitted): ColumnName
I have a table in SQL Server which looks like this: ID Code Name
I have a MySQL query that looks like this: UPDATE `Table` SET `Column` =
I currently have a table which only has a single editable column. I have
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I currently have a table of concentrations, which are linked to a table of
I currently have a table called paper , which holds all the information for
I have a table in MySQL 4.0 which currently has a year field as
I have an application in which I'm currently using a two column table in

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.