QueryDSL:意外的AST:org.hibernate.hql.internal.ast.tree.BetweenOperatorNode

时间:2018-11-19 06:07:37

标签: hibernate jpa querydsl

我应该使用 BETWEEN 通过JPA查询子句获得生日。
但不能使用“ .between”子句。
让我知道问题出在哪里。

Java8
SpringBoot2.0.1
HibernateEntityManager 5.2.17
QueryDSL4.1.4
MySQLConnector 5.1.46


MySQL查询

select floor(datediff(now(), birthDate) / 30) between 13 and 24
from Child


问题条款

JPAQueryFactory queryFactory = new JPAQueryFactory(this.em);
queryFactory
    .select(
        Expressions.numberTemplate(Long.class, "floor(datediff(now(), {0}) / 30)", child.birthDate)
        // above expression have no problem.
        .between(Expressions.constant(13), Expressions.constant(24))
    )
    .from(child).fetch();


错误消息

Unexpected AST: org.hibernate.hql.internal.ast.tree.BetweenOperatorNode 
 \-[BETWEEN] BetweenOperatorNode: 'between'
+-[METHOD_CALL] MethodNode: '('
|  +-[METHOD_NAME] IdentNode: 'floor' {originalText=floor}
|  \-[EXPR_LIST] SqlNode: 'exprList'
|     \-[DIV] BinaryArithmeticOperatorNode: '/' {dataType=org.hibernate.type.IntegerType@771b47f4}
|        +-[METHOD_CALL] MethodNode: '('
|        |  +-[METHOD_NAME] IdentNode: 'datediff' {originalText=datediff}
|        |  \-[EXPR_LIST] SqlNode: 'exprList'
|        |     +-[METHOD_CALL] MethodNode: '('
|        |     |  +-[METHOD_NAME] IdentNode: 'now' {originalText=now}
|        |     |  \-[EXPR_LIST] SqlNode: 'exprList'
|        |     \-[DOT] DotNode: 'child0_.birthDate' {propertyName=birthDate,dereferenceType=PRIMITIVE,getPropertyPath=birthDate,path=child.birthDate,tableAlias=child0_,className=com.example.Child,classAlias=child}
|        |        +-[ALIAS_REF] IdentNode: 'child0_.childId' {alias=child, className=com.example.Child, tableAlias=child0_}
|        |        \-[IDENT] IdentNode: 'birthDate' {originalText=birthDate}
|        \-[NUM_INT] LiteralNode: '30'
+-[NAMED_PARAM] ParameterNode: '?' {name=1, expectedType=org.hibernate.type.IntegerType@771b47f4}
\-[NAMED_PARAM] ParameterNode: '?' {name=2, expectedType=org.hibernate.type.IntegerType@771b47f4}


尝试了另一个条款

queryFactory.select(
    Expressions.booleanOperation(
        Ops.BETWEEN, 
        Expressions.numberTemplate(Long.class, "floor(datediff(now(), {0}) / 30)", child.birthDate), 
        Expressions.constant(13),
        Expressions.constant(24)
    )
)
.from(child)
.fetch();

0 个答案:

没有答案