How To Select literals in QueryDSL

安稳与你 提交于 2019-12-12 10:39:26

问题


Im currently working on a project that uses queryDSL and hibernate wherein it requires a select literal. 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. 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:

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? The NumberTemplate docs specifies T extending Number and Comparable but fails on non Integer types. How do i properly select constants/literals in querydsl?


回答1:


I've had a similar problem, and asked a developer of QueryDSL about it. the answer was : "It's a bug in Hibernate, we can't / won't fix it". You may have to use something else than an Expressions.constant to get it to work.

In my code, it works with a code similar to this :

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



来源:https://stackoverflow.com/questions/26234067/how-to-select-literals-in-querydsl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!