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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:30:04+00:00 2026-05-25T03:30:04+00:00

I got two classes User and Role which are mapped to each other using

  • 0

I got two classes User and Role which are mapped to each other using a many to many associations. Now I’m trying to query all users which doesn’t have a certain role using the Hibernate Criteria API. But I’m a little bit stuck.

To query users with a certain role I use the following query, which works just fine.

Session session = getSessionFactory().getCurrentSession();          
Criteria mainCrit = session.createCriteria(boClass);
return mainCrit.createAlias("roles", "r").add( Restrictions.eq("r.name", roleName)).list();

Now I’m a little bit confused how to reverse the query and get all user that don’t have a certain role. If possible I want to explicit exclude a certain role and don’t query for all roles chaining them with OR as there may be more roles added later dynamically.

UPDATE

To get a better understanding of my scenario you can see the association I’m trying to query in another question.

Furthermore I would also add that the name property of the Role class is an Enum, don’t know if this is important or changes the way to query the database.

  • 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-25T03:30:04+00:00Added an answer on May 25, 2026 at 3:30 am

    There’s perhaps a more elegant way, but the only one I’ve found is to query for all the users for which there doesn’t exist any role in the user’s roles which has the given role name.

    This is done like this:

    Criteria c = session.createCriteria(User.class, "user");
    
    DetachedCriteria roleOfUserWithName = DetachedCriteria.forClass(Role.class, "role");
    roleOfUserWithName.createAlias("role.users", "userOfTheRole");
    roleOfUserWithName.add(Restrictions.eqProperty("userOfTheRole.id", "user.id"));
    roleOfUserWithName.add(Restrictions.eq("role.name", roleName);
    roleOfUserWithName.setProjection(Projections.id());
    
    c.add(Subqueries.notExists(roleOfUserWithName));
    

    It’s equivalent to the following HQL query:

    select user from User user where not exists (
        select role.id from Role role inner join role.users userOfTheRole
        where userOfTheRole.id = user.id
        and role.name = :roleName);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got two classes (Students and Courses) which have a many-to-many-relationship. Instances of them
I have got two classes, one a subclass of the other (say Animal and
Let's say I've got two classes, each fully tested. There's duplication though, so I
I've got two classes : Unit and User. a Unit class has a User
I've got a simple web application with two persistent classes, User and Post, both
I've got a problem with UITableViews and many-to-many relationships. I have two classes, A
I have got two classes class Claim include Mongoid::Document embeds_many :claim_fields belongs_to :user ...
I've got these two classes which are to be part of a larger project,
I've got two Java projects, both generate Java classes based on a schema definition,
I got two projects running, both written in PHP. Now I want to merge

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.