简体   繁体   English

使用 WHERE 子句在一个查询中获取 @ElementCollection

[英]Fetch @ElementCollection in one query with WHERE clause

Here is the main table:这是主表:

@Entity
public class Group {

    @Id
    private Integer id;

    @ElementCollection(fetch = EAGER)
    @CollectionTable(name = "group_members",
            joinColumns = @JoinColumn(name = "group_id"))
    @Column(name = "email")
    private Set<String> members = new HashSet<>();

And the table which is ElementCollection in my case:在我的例子中是 ElementCollection 的表:

group_members {
int group_id,
varchar email
}

When I do a query with a join fetch, I get Group with all its Members in Set.当我使用 join fetch 进行查询时,我得到了 Group 及其所有成员在 Set 中。

But, when I tried to filter it by one of the members, I get Group only with ONE member, which I searched.但是,当我试图通过其中一名成员对其进行过滤时,我只得到了我搜索过的一个成员的群组。

Here is a query:这是一个查询:

@Query(value = "select group from Group group left outer join fetch group.members members where members = :email")
List<Group> findByMember(@Param("email") String email);

The main goal is that it should be only one query, without an N+1 issue.主要目标是它应该只有一个查询,没有 N+1 问题。 Thanks in advance!提前致谢!

The correct query, I believe, would be:我相信正确的查询是:

select distinct group from Group group 
 left join fetch group.members members 
 where :email member of group.members

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM