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
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.
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
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?
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()
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.