Prelude: I'm using hibernate 3 with a select that returns only some specified fields. I didn't find a way to get a list of POJOs, the best I found is to return a list of maps using query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
I have a list of Map<String, Object>
. Now, the way I found to convert it to a list of POJO is with Jackson:
ObjectMapper objectMapper = new ObjectMapper();
MyPojo myPojo;
List<myPojo> res = new ArrayList<>();
// rows is the List<Map<String, Object>>
for (Object row : rows) {
myPojo = objectMapper.convertValue(row, MyPojo.class);
res.add(myPojo);
}
Is there not a simpler way?
You was near, Yann :-)
I found it, thanks to cowtowncoder:
TypeReference<List<MyPojo>> typeReference = new TypeReference<List<MyPojo>>() {/* */};
List<MyPojo> res = mapper.convertValue(rows, typeReference);
Source: https://github.com/FasterXML/jackson/issues/89#issuecomment-882713171