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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:35:18+00:00 2026-05-23T11:35:18+00:00

I want to do a Jaas login module using JPA to store my AuthUser

  • 0

I want to do a Jaas login module using JPA to store my AuthUser and AuthUserRole. I’ll focus on the JPA side on this question.

Here is what I would do in the Database (not at all legitimate SQL code but hopefully comprehensive):

TABLE AuthUser( INT uid, VARCHAR password )
PRIMARY KEY (uid)

TABLE AuthUserRole( INT uid, INT role )
PRIMARY KEY (uid , role)
FOREIGN KEY (uid) REFERENCE AuthUser.uid

It makes sense to me, one role can only be assigned to a user once.

Here is what I attempted to do in Java, not showing username and email in AuthUser:

@Entity
public class AuthUser {
    @Id
    private int uid;

    private String password;

    @OneToMany
    private Set<AuthUserRole> roles;
}

@Entity
public class AuthUserRole {

    @Embeddedid
    private AuthUserRolePK authUserRolePK;
}

@Embeddable
public class AuthUserRolePK {
    public int uid;
    public int role;
}

Eclipselink does the mapping just fine, which means it works, but not the way I wanted. It makes a third table named *authuser_authuserrole* that holds the (AuthUser_uid , uid, role) columns. No need to mention AuthUser_uid and uid is identical.

The trivial answer would be:

@Entity
public class AuthUser {
    @Id
    private int uid;

    private String password;

    @OneToMany
    @JoinColumn(name="authUserRolePK.uid", referencedColumnName="uid")
    private Set<AuthUserRole> roles;
}

But EclipseLink cryes that when @Embeddedid is used, all primary key columns have to be mentioned.

Q: How do I make Eclipselink map my entities like the database schema I mentioned? Should I rather use @IdClass? I could see the result of a database –> entities mapping but that’s too easy 🙂

Thank you for your answers!

  • 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-23T11:35:19+00:00Added an answer on May 23, 2026 at 11:35 am

    Three tables is the typical way to do this actually, your approach is simply lacking a little bit of JPA finesse.

    Typically you have three tables associated as follows:

    AuthUser        AuthUser_Role (association)       Role
    frank     ----         frank,admin       -----    admin

    This is in fact what Eclipselink was trying to map for you, but in general, you don’t create the AuthUser_Role mapping yourself. Instead, you create a field on AuthUser like:

    @ManyToMany
    Set<Roles> userRoles
    

    And (optionally) on Role like:

    @ManyToMany(mappedBy="userRoles")
    Set<AuthUser> usersWithRole;
    

    Then EclipseLink takes care of the join table for you, and all you need to worry about is the userRoles field, which will update the join.

    You can extend this to create the join manually for say roles that start and end on a set date, etc, but for all but the most complex projects, that’s usually not necessary, and can often be accomplished in a different way. For compliance purposes, it’s easier to use one of the ELUG extensions to keep and access a history of changes for example, which is the most common reason I’ve seen for adding extra meta-data to the join information.


    Alternatively, if you REALLY want to do this with only two tables, make the AuthUserRole the primary side, and the AuthUser the passive side.

    @Entity
    public class AuthUser {
        @Id
        private int uid;
    
        private String password;
    
        @OneToMany(mappedBy="user")
        private Set<AuthUserRole> roles;
    }
    
    @Entity
    public class AuthUserRole {
    
        @Embeddedid
        private AuthUserRolePK authUserRolePK;
    }
    
    @Embeddable
    public class AuthUserRolePK {
        public AuthUser user;
        public int role;
    }
    

    In this arrangement, you will end up with only two tables, and the “user” field will indeed be equal to the uid of AuthUser in the database, it just shows up as an object on the Java side.

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

Sidebar

Related Questions

want to know why String behaves like value type while using ==. String s1
want to rewrite urls like site.com/software to wp-content/themes/dir/software.php and something is not working.. Here's
Want to verify that my understanding of how this works. Have a C++ Class
Want to build an web based Instant Messaging system similar to www.chatzy.com The question
want declarative transactional management example in spring aop........ Actually Here <aop:config> <aop:advisor advice-ref=addAdvice pointcut=execution(*
I want to use a temp directory that will be unique to this build.
want to drag element to specific div and if not dragged to this div
I'm using JAAS and have applied security on some folders for different roles. I
Want to check if this a good example for representing the abstract factory pattern.
Want to update page after jeditable (using gem on_the_spot) has been updated. Below works

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.