简体   繁体   English

如何在QueryDSL中选择文字

[英]How To Select literals in QueryDSL

Im currently working on a project that uses queryDSL and hibernate wherein it requires a select literal. 我目前正在开发一个使用queryDSL和hibernate的项目,其中需要一个select文字。 Following the examples posted here i have: 按照这里发布的示例我有:

createQuery().
   from(path()).
      where(specification().getPredicate()).
          list(
   ConstructorExpression.create(Foo.class, Expressions.constant(BigDecimal.ONE)));

where Foo class has a constructor which accepts a BigDecimal. 其中Foo类有一个接受BigDecimal的构造函数。 When running this in test, i get 在测试中运行时,我得到了

org.hibernate.QueryException: Parameters are only supported in SELECT clauses when used as part of a INSERT INTO DML statement
    at org.hibernate.hql.internal.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:146)

changing this to: 将此更改为:

createQuery()
   .from(path()).
       where(specification().getPredicate())
           .list(
ConstructorExpression.create(Foo.class, NumberTemplate.create(BigDecimal.class, "1.0")));

produces a different stacktrace: 产生不同的堆栈跟踪:

java.lang.IllegalArgumentException: argument type mismatch
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysema.query.types.ConstructorExpression.newInstance(ConstructorExpression.java:133)
    at com.mysema.query.jpa.FactoryExpressionTransformer.transformTuple(FactoryExpressionTransformer.java:50)

I tried changing the Foo class constructor to accept Integer and the query modified to use Integer as well for the sake of testing and it did produces the correct query: 我尝试将Foo类构造函数更改为接受Integer并将查询修改为使用Integer以便进行测试,它确实生成了正确的查询:

createQuery()
   .from(path()).
      where(specification().getPredicate())
         .list(ConstructorExpression.create(LevelBoundary.class, NumberTemplate.create(Integer.class, "1")));

Is using NumberTemplate the correct way to select BigDecimal literals? 使用NumberTemplate选择BigDecimal文字的正确方法是什么? The NumberTemplate docs specifies T extending Number and Comparable but fails on non Integer types. NumberTemplate docs指定T扩展Number和Comparable但在非Integer类型上失败。 How do i properly select constants/literals in querydsl? 如何在querydsl中正确选择常量/文字?

I've had a similar problem, and asked a developer of QueryDSL about it. 我遇到了类似的问题,并向QueryDSL的开发人员询问了这个问题。 the answer was : "It's a bug in Hibernate, we can't / won't fix it" . 答案是: “这是Hibernate中的一个错误,我们不能/不会修复它” You may have to use something else than an Expressions.constant to get it to work. 您可能必须使用Expressions.constant之外的其他内容才能使其工作。

In my code, it works with a code similar to this : 在我的代码中,它使用类似于这样的代码:

ConstructorExpression.create(LevelBoundary.class, Expressions.stringTemplate("'1'")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM