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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:40:16+00:00 2026-06-10T19:40:16+00:00

I have 3 tables Role, User, and UserRole. The table UserRole contains the mapping

  • 0

I have 3 tables Role, User, and UserRole. The table UserRole contains the mapping between user and role along with the two corresponding index columns. I am using hibernate with annotations, and would like to be able to “Revoke” a role from the user, but this is turning out to be somewhat difficult.

In my User Class I have

@ManyToMany(fetch=FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST}, targetEntity = Role.class)
    @IndexColumn(name = "role_index", base = 0)
    @NotFound(action=NotFoundAction.IGNORE)
    @JoinTable(name = "tblUser_Role", joinColumns={
    @JoinColumn(name = "UID")}, inverseJoinColumns={
    @JoinColumn(name = "roleid", nullable = false)})
    private List<Role> roles = new ArrayList<Role>(0);

In my Role class I have

@ManyToMany(fetch=FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST}, mappedBy="roles")
    private List<User> users = new ArrayList<User>(0);

and the DAO method I am calling to “Revoke” the role(s) is

@Override
    public boolean revokeRolesFromUserAccount(User user, List<Role> userRoles) {
        if (log.isInfoEnabled()) {
            log.info("Roles revoked from the User " + user.getUsername());
        }
        if (user == null) {
            return false;
        }
        if (userRoles == null) {
            return false;
        }

        Iterator<Role> iter = userRoles.iterator();
        List<Role> newroles = new ArrayList<Role>(0);
        Role role = null;
        while (iter.hasNext()) {
            role = (Role) this.sessionFactory.getCurrentSession().load(
                    Role.class, iter.next().getRoleid());
            newroles.add(role);
        }

        User newUser = null;
        newUser = (User) this.sessionFactory.getCurrentSession().load(User.class, user.getUid());
        newUser.getRoles().removeAll(newroles);
        this.sessionFactory.getCurrentSession().saveOrUpdate(newUser);
        return true;

    }

for some reason this does not work as expected, when breaking through I noticed the roles were not being initialized I guess due to the LazyLoading, and I tried doing something like Hibernate.initialize(newUser.getRoles()) but this did not change anything. I am still learning the ropes with hibernate and am not sure what I am missing, maybe something very obvious?? Thank you so much for your time and thoughts in advance!

UPDATE: After trying the fixes suggested by Paujo and Matin Kh and doing further debugging I still have not seen any differences in the Roles being loaded in after line newUser = (User) this.sessionFactory.getCurrentSession().load(User.class, user.getUid());

Here is a copy of my tblUser_Role, not sure if this helps. Thanks again!

(Adding roles works just fine)

enter image description here

  • 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-10T19:40:18+00:00Added an answer on June 10, 2026 at 7:40 pm

    Finally got this working, mostly by sheer luck, this is what I ended up with in my RoleDaoImpl class.

    @Override
        @Transactional
        public boolean revokeRolesFromUserAccount(User user, List<Role> userRoles) {        
            if (user == null) {
                return false;
            }
            if (userRoles == null) {
                return false;
            }   
    
            User newUser = null;
            newUser = (User) this.sessionFactory.getCurrentSession().load(User.class, user.getUid());   
    
            List<Role> newRoleList = newUser.getRoles();
    
            newRoleList.removeAll(userRoles);
    
            if(newUser.getRoles().retainAll(newRoleList)){
                if (log.isInfoEnabled()) {
                    log.info("Roles revoked from the User " + user.getUsername());
                }
                this.sessionFactory.getCurrentSession().saveOrUpdate(newUser);          
                return true;
            }else{
                return false;
        }
    

    Hope this can be of some use to others in the future!

    Thanks again for everyone’s help!

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

Sidebar

Related Questions

I have user and user_role tables. The user_role table has two columns: user_id role_name
Imagine we have two tables in the database, user (FK id_role) and role (PK
I have two tables named USER and ROLE with entities as USER and ROLE
I have tables linked by FK, I query on the first table using entity
I have a common User / Role setup, with a user_role join table. I'm
I have a database with a User's and Role's table. Each user can be
I have a table that has two different many-to-many relations to two different tables.
I have the following database tables: Table1: User UserId Username Table2: Role RoleId Rolename
I have a database schema like follows: User 1 ---- * UserRole *----1 Role
I have 3 simple tables: user, role, user_x_role with Many-To-Many relation. I have 2

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.