Criteria 查询,查找我不属于的所有组

发布于 2024-12-04 23:58:05 字数 1525 浏览 0 评论 0原文

我有两个实体:Group 和 GroupMember。组听起来就像是一个具有名称和其他一些属性的组。组的成员与 GroupMember 实体映射,该实体有一个条目,其中包含用户所属的每个组和组。它们如下所示:

@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;
}

我正在尝试编写一个条件查询,该查询返回具有某些预定义属性且当前用户不属于其中的所有组。我的查询如下所示:

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);           

这里的用户代表当前用户。此查询不起作用,因为如果还有其他成员与我属于同一组,则由于加入,它们将包含在查询结果中。正确的查询应该是什么样的?我对条件查询 API 还不太熟悉。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南渊 2024-12-11 23:58:06

ToMany 关系的连接在概念上是一个“anyOf”操作,这意味着如果其中任何一个为 true,则表达式为 true。

你想要的是一个“allOf”,标准没有这个,你需要在SQL中使用子选择。

JPQL 是,

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

条件是相同的,使用存在的子条件查询。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文