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?