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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:43:35+00:00 2026-05-25T15:43:35+00:00

I have two entities, Group and GroupMember. Group is like it sounds a group

  • 0

I have two entities, Group and GroupMember. Group is like it sounds a group that has name and some other properties. Members of the group are mapped with GroupMember entity, which has an entry with a User and a Group for every group the user is member of. They look like the following:

@Entity
@Table(name = EntityTokens.GROUP_TABLE)
public class Group
{
   @Id
   @Column(name = EntityTokens.GROUP_ID_COLUMN) 
   @GeneratedValue(strategy = GenerationType.IDENTITY)    
   private Long groupId;

   ...

   // Group members
   @OneToMany(orphanRemoval = true, fetch = FetchType.LAZY, mappedBy = "group", cascade = {CascadeType.ALL})
   private Collection<GroupMember> groupMembers;
}

@Entity
@Table(name = EntityTokens.GROUP_MEMBER_TABLE)
public class GroupMember
{
   @Id
   @Column(name = EntityTokens.GROUP_MEMBER_ID_COLUMN) 
   @GeneratedValue(strategy = GenerationType.IDENTITY)    
   private Long memberId;

   ...

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = EntityTokens.GROUP_MEMBER_ID_COLUMN)
   private Group group;
}

I’m trying to write a criteria query that returns all groups that have some predefined properties and that the current user is not part of. My query looks like this:

CriteriaQuery<Group> q = cb.createQuery(Group.class);
Root<Group> root = q.from(Group.class);

Join<Group, GroupMember> groups = root.join(Group_.groupMembers);

q.select(root);
q.where(cb.notEqual(groups.get(GroupMember_.user), user),
    cb.equal(root.get(Group_.global), false),
    cb.equal(root.get(Group_.personal), false),
    cb.equal(root.get(Group_.privacy), GroupPrivacy.PUBLIC));
q.distinct(true);           

The user here represents the current user. This query doesn’t work because if there are other members that are part of the same group as me they will be included in the query result due to the join. How should the correct query look like? I’m not that familiar with the criteria query API yet.

  • 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-25T15:43:36+00:00Added an answer on May 25, 2026 at 3:43 pm

    join for a ToMany relationship is conceptually an “anyOf” operation, meaning if any of the many is true then the expression is true.

    What you want is an “allOf”, criteria does not have this, you need to use a sub select for this in SQL.

    The JPQL would be,

    Select g from Group g where not exists (select m from g.members m where m.user = :user)
    

    The criteria would be the same, using a sub criteria query for the exists.

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

Sidebar

Related Questions

I have two entities that are something like that: public class Movie { public
I have two entities that have a many-to-many relationship; they are mapped with annotations
I have two entities: patient and checkpoint. Patient has attributes such as DOB, name,
I have two entities, one called Group and the other called Contact . They
I have two Entities: Group and Member : Entity name: Group Relationships: Member (inverse:Group)
Let's say I have two entities: Group and User. Every user can be member
I have two entities Visita and Cliente that implements respectively two interfaces IVisita and
I have two entities like this: public class Service { public virtual int ServiceId
I have two entities: Patient and Job. Patient has a to-many relationship to Job
i have two Entities, Parent and Child, that are linked through a bidirectional one-to-many

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.