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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:57:01+00:00 2026-06-02T03:57:01+00:00

I have a bit of a complicated structure that I have listed below. Basically

  • 0

I have a bit of a complicated structure that I have listed below. Basically I have a bunch of different “role groups”. Each role group contains a group of roles. A user can potentially have several different role groups, and a role group can be applied to many different users. Because of that I went with a ManyToMany relationship.

I want the RoleGroup to be part of the User, as listed below.

Everything is somewhat working. However, when I use the insert statements listed below I have a bug in my code. The RoleGroup contains only ROLE3. I am not sure why but somehow my code is only allowing one Role to be part of a RoleGroup with the current setup. Querying the database yields that all 3 Roles are present, but they are not on my Entity.

Please help!

CREATE TABLE USER (
    USER_ID   NUMBER(38, 0) PRIMARY KEY,
    NAME      VARCHAR2(50) NOT NULL
);

CREATE TABLE ROLE_GROUPS (
    ROLE_GROUP_ID   NUMBER(38, 0) PRIMARY KEY,
    NAME            VARCHAR2(50) NOT NULL
    -- constraints
    CONSTRAINT UQ_ROLE_GROUP_NAME UNIQUE (NAME)
);

CREATE TABLE ROLE (
    ROLE_GROUP_ID   NUMBER(38, 0) NOT NULL,
    ROLE            VARCHAR2(10) NOT NULL,
    -- constraints
    CONSTRAINT PK_ROLES PRIMARY KEY (ROLE_GROUP_ID, ROLE),
    CONSTRAINT FK_ROLES FOREIGN KEY(ROLE_GROUP_ID) REFERENCES ROLE_GROUPS
);

CREATE TABLE ROLE_GROUP_X_USER (
    ROLE_GROUP_ID   NUMBER(38, 0) NOT NULL,
    USER_ID         NUMBER(38, 0) NOT NULL,

    -- constraints
    CONSTRAINT PK_1 PRIMARY KEY (ROLE_GROUP_ID, USER_ID),
    CONSTRAINT FK_1 FOREIGN KEY(ROLE_GROUP_ID) REFERENCES ROLE_GROUPS,
    CONSTRAINT FK_1 FOREIGN KEY(USER_ID) REFERENCES USER
);

INSERT INTO ROLE_GROUPS(ROLE_GROUP_ID, NAME) VALUES (1, 'GROUP_1');
INSERT INTO ROLE(ROLE_GROUP_ID, NAME) VALUES(1 ,'ROLE1');    
INSERT INTO ROLE(ROLE_GROUP_ID, NAME) VALUES(1 ,'ROLE2');    
INSERT INTO ROLE(ROLE_GROUP_ID, NAME) VALUES(1 ,'ROLE3');    
INSERT INTO ROLE_GROUP_X_USER(USER_ID,ROLE_GROUP_ID) SELECT USER_ID, 1 FROM USER where USER_ID IN ('1', '2', '3', '4');

@Entity
@Table(name="USER")
public class User {
   //Other code

   @ManyToMany(cascade = CascadeType.ALL, fetch=FetchType.EAGER)
   @JoinTable(name = "ROLE_GROUP_X_USER", 
   joinColumns = { @JoinColumn(name = "USER_ID") }, 
   inverseJoinColumns = { @JoinColumn(name = "ROLE_GROUP_ID") })
   private Set<RoleGroupEntity> roleGroupEntities;
}

@Entity
@Table(name="ROLE_GROUPS")
public class RoleGroupEntity {
   //Other code

   @ElementCollection(fetch=FetchType.EAGER)
   @CollectionTable(name="ROLE", joinColumns=@JoinColumn(name="ROLE_GROUP_ID"))
   @AttributeOverrides({
   @AttributeOverride(name="role", column=@Column(name="ROLE")) })
   private Set<Role> roles = new HashSet<Role>();
}


@Embeddable
public class Role {
    private String role;
    //Getter, Setter
    //Hashcode, equals override
}
  • 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-02T03:57:03+00:00Added an answer on June 2, 2026 at 3:57 am

    It looks like your joinColumn is wrong. Usually you have two columns on a ManyToMany Join, joinColumns and inverseJoinColumns, but I’d have to say you’re using the wrong column.

    I think you’re going to need a separate mapping table for including roles in groups. Add this to your RolesGroup class:

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "GROUP_ROLE_MAP_T", joinColumns = {
            @JoinColumn(name = "ROLE_GROUP_ID")
    }, inverseJoinColumns = {
            @JoinColumn(name = "ROLE_ID")
    })
    private Set<Role> role = new HashSet<Role>(0);
    

    Make your roles a table too:

    @Entity
    @Table(name = "ROLES_T")
    @AttributeOverride(name = "id", column = @Column(name = "ROLE_ID"))
    public class Role {
        @Column(name = "ROLE_ID", nullable = true)
        private Long roleId = new Long(0);
    
        @Column(name = "ROLE_NAME", nullable = false)
        private String role;
    }
    

    Don’t forget your getter/setters.

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

Sidebar

Related Questions

i have bit of code that causes an underflow: var t1, t2, delta: DWORD:
I have a bit of Javascript code that creates a save friendly version of
I have a bit of a situation. I've consumed about fourty-eleven different tutorials/books/videos on
I have this bit of JavaScript... 15 $('.ajax_edit_address').each(function() { 16 $(this).ajaxForm({ 17 target: $(this).parents('table.address').find('tr.address_header').children(':first'),
I have a bit of a complicated sql query I need to do, and
I have a little bit complicated query. I have table tbl_Categories {CategoryID, Name, CategoryId_fk}
This is a bit complicated so bear with me: On FORM.php, I have 2
I have some things to do, but is bit complicated for me.I need some
The question is a bit complicated so I will try and explain. I have
I have a question regarding how initialization vector works on cryptography. I have bit

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.