views:

91

answers:

1

Hi, The first part is HOW TO PROJECT COLLECTIONS?

Can We apply projections on collection elements? For e.g

    class User{
    private address List<Address>;
}

class Address{
    private String city;
    private String state;
}

Now can I just load the address attribute of User class? Using code like :

criteria.setProjection(Projections.property("Address"));

But it always return me null, even when the object does have an address field set. Is there any different way to project Collection Items??

THE SECOND PART : NESTED PROJECTIONS. Consider the same model as shown above, but instead of having a collection of Address, there is an single element. Now What if I want to just load the "city" attribute of Address which is an part of User class??

I tried doing :

Projections.property("Address.City")

But it gives me error, stating could not resolve property: "Address.City" of User.

Is there any provision for Projecting Collection elements and Nested Projections??

+1  A: 

Hibernate is case sensitive. so instead of *A*ddress.*C*ity, use *a*ddress.*c*ity. If it does not work, try to use alias, like :

DetachedCriteria criteria = DetachedCriteria.forClass(User.class);
criteria.createAlias("address", "addAlias");
criteria.setProjection(Projections.property("addAlias.city"));
Thierry
Will try that.What about Projecting Collections???
KCore
that should work the same way (to be sure it is supported, look at the hibernate reference manual for your hibernate version). Be careful there is in your example the same 'case' problem : use 'address' for the Projections.property, not 'Address'.
Thierry