Java HQL检查对象是否包含请求集的所有元素
我有两个实体。例如,向其发布和标记。 我必须编写一个方法,它将只接收帖子,其中包含查询中提到的所有标记 我试过了Java HQL检查对象是否包含请求集的所有元素,java,spring,jpa,hql,spring-data-jpa,Java,Spring,Jpa,Hql,Spring Data Jpa,我有两个实体。例如,向其发布和标记。 我必须编写一个方法,它将只接收帖子,其中包含查询中提到的所有标记 我试过了 @Query("select distinct p from posts p join p.tags t where t in ?1") Page<Post> findDistinctByTagsIn(Set<Tag> tagSet, Pageable pageable); @Query(“从posts p join p.tags t中选择不同的p,其中t在
@Query("select distinct p from posts p join p.tags t where t in ?1")
Page<Post> findDistinctByTagsIn(Set<Tag> tagSet, Pageable pageable);
@Query(“从posts p join p.tags t中选择不同的p,其中t在?1中”)
PageFindDintintByTagsin(设置标记集,可分页);
但是,如果至少有一个标记包含在标记集中,则它将Post
我如何仅使用HQL和JPA存储库来解决这个问题
UPD:
@ManyToMany(fetch=FetchType.EAGER,cascade=CascadeType.ALL)
@JoinTable(name=“posts\u tags”,joinColumns={
@JoinColumn(name=“post_id”,nullable=false,updateable=false)},
inverseJoinColumns={@JoinColumn(name=“tag_id”)})
公共集getTags(){
返回标签;
}
添加到您的标签
下一节课多通
关系:
@Entity
public class Tag{
...
private Post post;
@ManyToOne
@JoinTable(name = "posts_tags",
joinColumns = {@JoinColumn(name = "tag_id")},
inverseJoinColumns = {@JoinColumn(name = "post_id")})
public Post getPost(){
return post;
}
...
}
让我们尝试构建查询
我们不需要那些标签不在我们标签列表中的帖子。我们将在下一次查询中选择它们:
select t.post from Tag t where t not in (:tagSet) and t.post is not null
我们不需要帖子,没有任何标签。让我们也选择它们:
select p from Post p where p.tags is empty
现在让我们将我们的查询合并在一起:
select p from Post p where
p not in (select t.post from Tag t where t not in (:tagSet) and t.post is not null)
and p not in (select p2 from Post p2 where p2.tags is empty)
您可以使用命名参数绑定此查询:
Page<Post> findDistinctByTagsIn(@Param("tagSet") Set<Tag> tagSet, Pageable pageable);
Page findDistinctByTagsIn(@Param(“tagSet”)Set标记集,可分页;
添加到您的标签
下一节课多通
关系:
@Entity
public class Tag{
...
private Post post;
@ManyToOne
@JoinTable(name = "posts_tags",
joinColumns = {@JoinColumn(name = "tag_id")},
inverseJoinColumns = {@JoinColumn(name = "post_id")})
public Post getPost(){
return post;
}
...
}
让我们尝试构建查询
我们不需要那些标签不在我们标签列表中的帖子。我们将在下一次查询中选择它们:
select t.post from Tag t where t not in (:tagSet) and t.post is not null
我们不需要帖子,没有任何标签。让我们也选择它们:
select p from Post p where p.tags is empty
现在让我们将我们的查询合并在一起:
select p from Post p where
p not in (select t.post from Tag t where t not in (:tagSet) and t.post is not null)
and p not in (select p2 from Post p2 where p2.tags is empty)
您可以使用命名参数绑定此查询:
Page<Post> findDistinctByTagsIn(@Param("tagSet") Set<Tag> tagSet, Pageable pageable);
Page findDistinctByTagsIn(@Param(“tagSet”)Set标记集,可分页;
文章和标签在数据模型中是如何相互链接的?我们需要知道这些实体或基础表的相关部分。@M助记符、标记和贴子是多对多链接的。在数据模型中,贴子和标记是如何相互链接的?我们需要知道这些实体或基础表的相关部分。助记符、标记和贴子是多对多链接的