We're updating the issue view to help you get more done. 

Issue with CriteriaBuilder.literal(boolean) in my select expression?

Description

Here is how i build my Select using JPA criteria API, builder bein my CriteriaBuilder

1 2 3 4 5 6 7 8 9 10 11 12 CriteriaQuery<MyDto> crit = builder.createQuery(MyDto.class).distinct(true); Root<MyEntity> root = crit.from(MyEntity.class); List<Expression<?>> listFields = new ArrayList<>(); listFields.add(root.get("intField")); listFields.add(root.get("stringField")); listFields.add(builder.<Boolean> selectCase().when( builder.isTrue(root.<Boolean> get("booleanField")), true).otherwise(false)); listFields.add(builder.literal(false)); Expression[] arraySelect = listFields.toArray(new Expression[listFields.size()]); crit.multiselect(arraySelect);

And My obj has a constructor :

1 2 3 public MyDto(Long pIntAttr, String pStringAttr, boolean pBooleanAttr1, boolean pBooleanAttr2) { // Build my item }

I was using hibernate 4 and had a nullpointer on the handler so after looking in the net, and seeing there was now a BooleanValueHandler in Hibernate 5, I upgraded.

now the error i get is :

My title

Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Unable to locate appropriate constructor on class [mypackage.MyDto]. Expected arguments are: long, java.lang.String, boolean [select distinct new mypackage.MyDto(generatedAlias0.intField, generatedAlias0.stringField, case when generatedAlias1.booleanField = true then true else false end, false) from myEntityPackage.MyEntity as generatedAlias0]

If i replace the literal(false) by literal(0) and I change the type of pBooleanAttr2 to Integer in the constructor, it works fine.

If I keep the boolean but remove it from the constructor, it tells me i have 4 fields in my select and only 3 in the constructor.

So what am I missing ?

Of course my query is more complex than this.

Environment

Hibernate-entitymanager 5.1.0.final with hibernante-jpa-2.1-api

Status

Assignee

Andrea Boriero

Reporter

WilliamB

Fix versions

backPortable

None

Suitable for new contributors

None

Requires Release Note

None

Pull Request

None

backportDecision

None

Components

Affects versions

5.2.0
5.1.0
5.0.9

Priority

Blocker