gpt4 book ai didi

querydsl - 在 QueryDsl 中编写条件查询

转载 作者:行者123 更新时间:2023-12-03 08:56:38 34 4
gpt4 key购买 nike

我有一个实体,其中包含一个名为 date 的字段和一个名为 creationDate 的字段。第一个可以为空,后者不能为空。

现在我想获取特定时间范围内的所有项目。如果date不为空,则使用date。如果为空,那么我们应该使用creationDate作为检查的备份。

为了实现这一目标,我尝试组装一个 com.querydsl.core.types.Predicate,然后将其传递给我的数据存储库 findAll 方法。 (我正在使用 Spring 数据存储库)。

到目前为止我所拥有的:

    QItem item = QItem.item$;
BooleanExpression predicate = new CaseBuilder()
.when(item.date.isNotNull())
.then((Predicate) item.date.between(startDate, endDate))
.otherwise(item.creationDate.between(startDate, endDate));

我在使用我的和平代码时收到此异常:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: case near line 3, column 7 [select item$
from com.example.entities.Item item$
where case when (item$.date is not null) then (item$.date between ?1 and ?2) else (item$.creationDate between ?1 and ?2) end]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: case near line 3, column 7 [select item$
from com.example.entities.Item item$
where case when (item$.date is not null) then (item$.date between ?1 and ?2) else (item$.creationDate between ?1 and ?2) end]] with root cause

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: case near line 3, column 7 [select item$
from com.example.entities.Item item$
where case when (item$.date is not null) then (item$.date between ?1 and ?2) else (item$.creationDate between ?1 and ?2) end]
at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:74) ~[hibernate-core-5.2.17.Final.jar:5.2.17.Final]

这里出了什么问题?

我使用 CaseBuilder 的目的是否正确?

问候

最佳答案

好的,我用类似的内容替换了基于大小写的谓词

Predicate withDate = new BooleanBuilder(item.date.isNotNull())
.and(item.date.between(startDate, endDate));
Predicate withoutDate = new BooleanBuilder(item.date.isNull())
.and(item.creationDate.between(startDate, endDate));

Predicate combined = new BooleanBuilder(withDate)
.or(withoutDate);

并且它有效。我仍然对我的第一种方法有什么问题感兴趣。

关于querydsl - 在 QueryDsl 中编写条件查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54846462/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com