# "Missing constructor" error when using multiselect

**URL:** <https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734>\
**Category:** Hibernate ORM\
**Created:** [October 7, 2022, 8:53am UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734 "2022-10-07T08:53:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [October 7, 2022, 8:53am UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/1 "2022-10-07T08:53:27Z")

</div>

Hi there,

we just upgraded from Hibernate 3.6 to 5.6 and field projection seems to work a little differently than we were used to.

This is my DAO code:

```auto
public List<ENTITY> findByCriteria(final Map<String, Object> criteriaMap, final List<String> fields, final Class<ENTITY> entityClass) {

CriteriaBuilder cb = session.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(aliasToBeanClass); // aliasToBeanClass = PmPropertyVOImpl.class
Root root = cq.from(aliasToBeanClass);

if (fields != null) {
	List<Selection> projections = new ArrayList<>();
	for (String field : fields) {
		projections.add(root.get(field));
	}

	if (keys.contains(AbstractBaseProperties.DISTINCT)) {
		cq.multiselect(projections).distinct(true);
		} else {
			cq.multiselect(projections).distinct(false);
		}
        }
}

```

When I fetch the data like this

```auto
cq.where(cb.and(predicates.toArray(new Predicate[0])));
Query query;
if (fields != null) {
	query = session.createQuery(cq);
} else {
	query = session.createQuery(cq);
}
List<ENTITY> entities = query.getResultList();

```

Just for the sake of debugging i set a breakpoint to the query line following the if-clause. When fields is not null and the programmer calling the findByCriteria method wants just a reduced set of attributes from that table, I get the message

```auto

Unable to locate appropriate constructor on class [com.myapp.pm.business.impl.PmPropertyVOImpl]. Expected arguments are: long

```

My problem is that we want to keep this method dynamic, allowing developers to pass every combination of fields that they want - or no fields at all if they want the entire row. Does that mean I would have to create constructors for every possible combination of fields (which is literally impossible). In the case that led to the error message above, I wanted to fetch the `rowguid` only. my class PmPropertyVOImpl has a public non-args constructor and getter/setter for every attribute.

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 7, 2022, 10:26am UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/2 "2022-10-07T10:26:29Z")

</div>

Calling `multiselect` essentially instructs Hibernate to create a dedicates select clause containing just the expressions you pass. This kind of multiselect query usually can only result in the types `Object[]` and `Tuple`. So in your case, the type `aliasToBeanClass` that you pass into `cb.createQuery`, normally has to be either `Object[]` or `Tuple`, but Hibernate also supports implicit constructors i.e. if the type is other than `Object[]` or `Tuple`, it just assumes that you want to call a constructor of the result class, with the types matching the selection items.

I don’t know what Hibernate 3 did before, but the thing that you seem to want to achieve, would require that you assign aliases to the projections and then use the `AliasToBeanResultTransformer` by calling

```java
query.setResultTransformer( Transformers.aliasToBean( aliasToBeanClass ) )

```

and replacing `cb.createQuery( aliasToBeanClass )` with `cb.createQuery( Object[].class )`.

---

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [October 7, 2022, 2:09pm UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/3 "2022-10-07T14:09:32Z")

</div>

Ok that sounds promising but I think the resultTransformer is having some trouble in my case. I fetch data that should result in one result line, which is in my List entities, but it looks like the transformer is not able to do its thing, as all values are null. Do you have an idea, what can cause this problem?

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/b/b73703f55ed767b58f263eddb85299cd593da078.png)

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 7, 2022, 2:59pm UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/4 "2022-10-07T14:59:35Z")

</div>

You probably didn’t specify the aliases. You need to do something like this:

```java
	for (String field : fields) {
		projections.add(root.get(field).alias(field));
	}

```

---

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [October 7, 2022, 3:02pm UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/5 "2022-10-07T15:02:53Z")

</div>

I did that.

```auto
if (fields != null) {
			for (String field : fields) {
				Selection s = root.get(field);
				s.alias(field); // field = rowguid, setter in VO = setRowguid()
				projections.add(s);
			}

		}

```

Can i assume that hibernates looks for a first letter capitalled version of the setter?

For example, if my variable is rowguid, the setter is setRowguid, it should not try to look up setrowguid()

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 7, 2022, 3:19pm UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/6 "2022-10-07T15:19:03Z")

</div>

You’ll have to debug into `org.hibernate.transform.AliasToBeanResultTransformer#initialize` then to understand what is happening.

---

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [October 7, 2022, 9:31pm UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/7 "2022-10-07T21:31:51Z")

</div>

It seems like somewhere on the way my aliases get lost and are not available for the Transformer.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/4/4d389786b5d0579e1ffd24ff44772bfb3f6bf614.png)

I will do some more research and keep you guys updated in case I found the error. Thanks for helping me out so far.

---

<div class="post-metadata">

**Author:** ![d00d](https://avatars.discourse-cdn.com/v4/letter/d/49beb7/32.png) [@d00d](https://discourse.hibernate.org/u/d00d)\
**Post date:** [October 10, 2022, 7:27am UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/8 "2022-10-10T07:27:33Z")

</div>

I think this is a bug in 5.6, in this ticket the same issue was fixed in 6.0.0 Beta 1

[https://hibernate.atlassian.net/browse/HHH-13140](https://hibernate.atlassian.net/browse/HHH-13140)

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [October 10, 2022, 8:27am UTC](https://discourse.hibernate.org/t/missing-constructor-error-when-using-multiselect/6734/9 "2022-10-10T08:27:50Z")

</div>

You can workaround this by providing a custom implementation of the `AliasToBeanConstructorResultTransformer` that injects aliases into the array based on fields array/list you pass to your method.
