Reputation: 1622
I have a POJO that looks something like this
@AllArgsConstructor
public class POJO {
@Getter
@Setter
private String name;
@Getter
@Setter
private int age;
}
I use this POJO to set values from a hql query as in
session.createQuery("Select new fullyQualifiedName.POJO(emp.name, emp.age) From Employee emp where emp.id = 1");
Recently, I've been asked to make a request to add a List property to the POJO. As in,
@AllArgsConstructor
public class POJO {
@Getter
@Setter
private String name;
@Getter
@Setter
private int age;
@Getter
@Setter
private List<Hobby> hobbies;
}
However, at this point, I do not know how to join Hobby table, get values as a list and pass to the constructor.
What are the possible ways to accomplish this?
Upvotes: 0
Views: 317
Reputation: 139
I don't think you can achieve what you want with the new
keyword anymore. Maybe others might be more helpful on this construct but I have not seen any example providing a collection with the new
keyword, yet. I think this construct has some limitations.
As for the solution I propose:
Change your query to Select emp, h From Employee emp left join Hobby h where emp.id = 1
. You can also use inner join depending on your use case.
Set a custom ResultTransformer to your query object. Something like:
query.setResultTransformer(
new ResultTransformer() {
@Override
public Object transformTuple(Object[] tuple, String[] aliases) {
return tuple;
}
@Override
public List transformList(List collection) {
//Each member of collection is a Object[], we are working at row level
//You should process the collection, eliminate duplicates etc and return a
//List containing a single POJO object
}
}
);
Upvotes: 1