My question is same exact issue like this SO question.
Assume we have entity like
public class Employee {
Long getId();
String getName();
Address getAddress();
}
With the help of createCriteria and Projections we are trying to get id,name from users and city from address table.
The issue we are having is, city is not being set to address object city property.
Here is the code we tried:
Criteria cr = session.createCriteria(Employee.class)
.setProjection(Projections.projectionList()
.add(Projections.property("id"), "id")
.add(Projections.property("Name"), "Name")
.add(Projections.property("address.city"), "address.city"))
.addOrder(Order.asc("address.city"))
.createAlias("address", "address")
.setResultTransformer(Transformers.aliasToBean(Employee.class));
Based on JBNizet comment on above question I understood that partially objects are bad idea, but If I am mandated to do that how can I achieve it? Any input would be appreciated.
Use an AliasToBeanConstructorResultTransformer, and define a constructor like the following in your entity:
Or, (and this is my preferred solution, because it avoids reflection and allows refactoring the code with less risk) dont’t use any result transformer and construct your instances yourself: