Hibernate Criteria Projection of non scalar values
Asked Answered
T

0

1

Is there any way to project multiple values for an root entity object using Criteria?

Assume we have these classes (With the proper mappings):

   class Boss {
        private String name;
        private List<Employee> employees;

        // setters and getters and more stuff
   }

   class Employee {

        private String name;

        // setters and getters and more stuff
   }

Then i am trying to do this :

   public void test() {
        Criteria criteria = this.getSession().createCriteria(Boss.class);

        criteria.createAlias("employees","employees");

        ProjectionList projectionList = Projections.projectionList();
        projectionList.add(Projections.property("name"), "name");
        projectionList.add(Projections.property("employees.name"), "subordinatesNames"); 
        criteria.setProjection(projectionList);

        criteria.setResultTransformer(new AliasToBeanResultTransformer(BossBean.class));
        List<BossBean> results = criteria.list(); // fails here
        for (BossBean bossBean : results) {
            System.out.println (bossBean);
        }
    }

This is how the Bean looks like (nothign special, just for grouping values) :

public static class BossBean {
    private String name;
    private List<Strings> subordinatesNames;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Strings> getSubordinatesNames() {
        return subordinatesNames;
    }

    public void setSubordinatesNames(List<Strings> subordinatesNames) {
        this.subordinatesNames = subordinatesNames;
    }

}

The exception is this :

2014-06-06 13:37:38 [main] ERROR org.hibernate.property.BasicPropertyAccessor - expected type: java.util.List, actual value: java.lang.String.

I Guess is trying to fit the String returned from Boss(root object) -> (A)Employee(association) ->name(value) into a List.

I want to auto magically get all inserted in the List. Is there a way to achieve this using Criteria? If not, how i can achieve it?

Thanks in advance!

Grettings

Víctor

Tedtedd answered 6/6, 2014 at 17:34 Comment(5)
Guys i found that this post, i guess, is asking the same think : #14971024. I suspect that is no way to do this with plain Criteria.Tedtedd
You'll have to iterate on the [String, String] tuples and construct the ojects by yourself. But why don't you simply get Boss instances and fetch their employees? What's the point of the projections, if the goal is to recreate the same structure as the one of the entities?Mononucleosis
Yes that approach is valid. But the goal was just trying to retrieve all the data in only one query, in a more simple way, and put it on a bean to use in a jsp, that is detached from hibernate session. BTW : I has the doubt about how good is to send a domain object (attached to an hibernate session) to a view (jsp). Correct me, if you know that is no problem at all. Thanks!Tedtedd
Remember that iterating thought each Object may result in multiple queries, one for each object, if the object manages the association in a lazy way. So the point was also for performance. But again, my mind is opened to receive tips and more tips! Got plenty of points to give!Tedtedd
There's no probkel in doing that. That's the recommended way. Simply fetch all you need to avoid too many queries: select distinct boss from Boss boss left join fetch boss.employeesMononucleosis

© 2022 - 2024 — McMap. All rights reserved.