java - QueryDsl - 根据嵌套数组内容排除结果
问题描述
我正在尝试使用 QueryDsl 获取所有不包含特定类别的帖子
我的模型定义为:
邮政
@QueryEntity
@Table(name = "posts")
public class PostEntity implements {
@Id
@Column(name = "id")
private String id;
@OneToMany
@JoinTable(
name = "post_categories",
joinColumns = @JoinColumn(name = "post_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "category_id", referencedColumnName = "id")
)
private List<CategoryEntity> categories;
}
类别
@QueryEntity
@Table(name = "categories")
public class CategoryEntity {
@Id
@Column
private String id;
}
(为简洁起见,省略了一些 Lombok 注释)
两者通过post_categories
连接表关联,以使用类别标记帖子。
我尝试使用与此类似的查询来排除分类为news
:
var query = QPostEntity
.postEntity
.categories.any().id.notIn("news");
但是,这仍然会返回该类别中的帖子 - 我让它正常工作的唯一方法是在notIn
声明中包含所有帖子类别。
问题:如何查询不包含特定类别的帖子?
更新#1
似乎上面的查询生成类似于
where exists(
select 1 from post_categories where category_id not in ('news')
)
其中还包括所有其他类别的帖子。我发现以下查询确实产生了正确的结果(在语句not
之前移动):exists
where not exists(
select 1 from post_categories where category_id in ('news')
)
这可以通过将 querydsl 重写为:
.categories.any().id.in("news").not();
然而,这似乎非常令人困惑。有什么更好的方法吗?
解决方案
我会尝试用子查询来解决这个问题。你可以试试下面的吗?
SubQueryExpression<String> subquery = JPAExpressions.select(QCategoryEntity.categoryEntity.id)
.from(QCategoryEntity.categoryEntity)
.where(CategoryEntity.categoryEntity.eq("news"));
return new JPAQueryFactory(em)
.select(QPostEntity.postEntity)
.from(QPostEntity.postEntity)
.innerJoin(QPostEntity.postEntity.categories)
.where(QCategoryEntity.categoryEntity.id.notIn(subquery));
可能您没有使用JPAQueryFactory
... 如果没有,您能否分享您实际执行查询的方式?
推荐阅读
- php - 需要 Laravel 包的详细信息
- angular - 使用类绑定在 Angular 中激活 routerLink
- php - 自动加载 - 致命错误:未捕获错误:类
- angularjs - Angular树控制----过滤树的节点
- angularjs - 我如何在函数外部获得响应,内部函数响应获取
- opencv - 移除影响图像的照明形状以保留特征
- nginx - 无法让 Nginx 在同一根目录的不同位置工作
- python - 如何使用 python 访问 Gerrit Rest API
- php - 当我尝试使用 set_error_handler 时,Laravel 出现 unserialize() 错误
- pdf - 如何在 PowerBuilder 中动态创建 PDF 文档?