Projections for Collections and Nested Projections
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开发者_如何学JAVA any provision for Projecting Collection elements and Nested Projections??
Hibernate is case sensitive. so instead of Address.City, use address.city. 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"));
精彩评论