Nambari Nambari - 2 months ago 14
Java Question

Hibernate partially populated object with projections

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)
.add("id"), "id")
.add("Name"), "Name")
.add(""), ""))
.createAlias("address", "address")

Based on
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:

public Employee(Long id, String name, String city) { = id; = name;
    this.address = new Address();

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:

List<Object[]> rows = criteria.list();
List<Employee> result = new ArrayList<Employee>(rows.size());
for (Object[] row : rows) {
    Employee e = new Employee();
    e.setId((Long) row[0]);
    e.setName((String) row[1]);
    Address address = new Address();
    address.setCity((String) row[2]);