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

  • Home
  • SEARCH
  • 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 894453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:23:25+00:00 2026-05-15T14:23:25+00:00

I have two tables: Users and Roles. A user may have more roles, so

  • 0

I have two tables: Users and Roles. A user may have more roles, so this is a ManyToMany relationship.
I’ve generated the entity classes with Netbeans, and they look like this:

@Table(name = "users")
public class Users implements Serializable {
    @Id
    @Basic(optional = false)
    @Column(name = "user_name")
    private String userName;
    @JoinTable(name = "user_roles", joinColumns = {
        @JoinColumn(name = "user_name", referencedColumnName = "user_name")}, inverseJoinColumns = {
        @JoinColumn(name = "role_name", referencedColumnName = "role_name")})
    @ManyToMany(fetch = FetchType.LAZY)
    private Collection<Roles> rolesCollection;

(etc)

@Table(name = "roles")
public class Roles implements Serializable {
    @Id
    @Basic(optional = false)
    @Column(name = "role_name")
    private String roleName;
    @ManyToMany(mappedBy = "rolesCollection", fetch = FetchType.LAZY)
    private Collection<Users> usersCollection;

Here is my problem: I’d like to list all users in a table, and for each user all the roles they have.
I couldn’t come up with a solution. How do I refer to the collections from within JPQL? How do I return a collection? Is this possible at all?

Even worse, I have my own class that I’d like to use to display the results on my JSF page. Here is the current query:

Query query = em.createQuery("SELECT NEW UserListQueryObject"
        + "(u.userName, ...)"
        + " FROM Users u",
        UserListQueryObject.class);
List<UserListQueryObject> users = query.getResultList();

It works for simple attributes and OneToOne relationships. But not for ManyToMany. I tried JOINing like this:

Query query = em.createQuery("SELECT NEW UserListQueryObject"
        + "(u.userName, ...., r.roleName)"
        + " FROM Users u JOIN u.rolesCollection r",
        UserListQueryObject.class);

… but the runtime complained about bad syntax near “r”, and also about r.roleName. And it wanted to return a single role, but I need all the roles for a user!

I’m not an expert at SQL either, so it is quite possible that the solution is simple… still after having read the “Pro JPA 2…” book I’m stuck. Thanks for any help.

  • 1 1 Answer
  • 1 View
  • 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-15T14:23:25+00:00Added an answer on May 15, 2026 at 2:23 pm

    I’d like to list all users in a table, and for each user all the roles they have. (…)

    If you want to read multiple objects in a single query, you can use a join fetch:

    SELECT u FROM Users u JOIN FETCH u.rolesCollection
    

    This will perform an INNER join which means that this will filter any Users from the result set that did not have roles.

    To avoid the above behavior, use an OUTER join:

    SELECT u FROM Users u LEFT JOIN FETCH u.rolesCollection
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple database with two tables. Users and Configurations. A user has
I have two tables: User and Roles. and User is mapped to roles using
Let's say you have two tables, Users and UserRoles. Here's how the two tables
I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
hope someone can help. I have two tables: Users -UserID -UserName UsersType -UserTypeID -UserID
If I have two tables - Logins and Users, as follows: Logins LoginIdNo UserIdNo
We have two tables - Tasks and TasksUsers (users assigned to task). Task has
I have Two tables. 1.Users table (Username , Name) 2.Picture table( ID , Username
Say we have two tables in an MS Access db: Service Users: | ID
Let's say I have two tables, users and notes. Let's say the schemas look

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.