gpt4 book ai didi

java - QueryDSL:QueryDSL 2.9.0 中 ConstructorExpression 和 CaseBuilder 的 JPA 示例

转载 作者:行者123 更新时间:2023-11-29 05:28:37 27 4
gpt4 key购买 nike

public class ProductDTO {
public ProductDTO(final String name, final Boolean isBrandNew) { ... }
}

QProduct product = QProduct.product

Expression<Boolean> isBrandNew = new CaseBuilder()
.when(product.creaDate.eq(product.modDate)).then(Boolean.TRUE)
.otherwise(Boolean.FALSE)
.as("brandNewProduct")

JPAQuery query = new JPAQuery (this.em);
query.from(product)
.where(product.name.eq("blah"))
.listDistinct(ConstructorExpression.create(ProductDTO
, product.name
, isBrandNew))

在我的查询的最终选择语句中包含“brandNewProduct”列的 CaseBuilder 的正确设置是什么?

现在,我从 hibernate 中得到的只是以下异常

    org.hibernate.hql.internal.ast.tree.ParameterNode cannot be cast to
org.hibernate.hql.internal.ast.tree.SelectExpression
org.hibernate.hql.internal.ast.tree.CaseNode.getFirstThenNode(CaseNode.java:43)

最佳答案

供将来引用...

已经有人发过类似问题herehere .根据 Timo 的说法,存在一个错误,或者说是 hibernate 强加的限制:

Best Answer Answered by Timo Westkämper on 2013-02-22T01:55:04-05:00 It is syntactically possible, but currently unsupported by Hibernate. Here is a related ticket https://github.com/mysema/querydsl/issues/185

因此,nithril 发布的解决方案实际上对我有用:

query().from(cat).list(cat.name.when("Bob").then(Expressions.numberTemplate(Long, "1")).otherwise(Expressions.numberTemplate(Long, "2")));

我最终使用了:

JPAQuery query = new JPAQuery (this.em);
query.from(product)
.where(product.name.eq("blah"))
.listDistinct(ConstructorExpression.create(ProductDTO
, product.name
, product.modDate.when(product.creaDate)
.then(Expressions.booleanTemplate("true"))
.otherwise(Expressions.booleanTemplate("false"))

))

希望这对其他人有同样问题的情况有所帮助。

关于java - QueryDSL:QueryDSL 2.9.0 中 ConstructorExpression 和 CaseBuilder 的 JPA 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21880923/

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