-1
To show the list of a register in my system, I populate the query in a DTO as below. My question is: There is a simpler way to popular the query in the DTO list?
public List<ObjetoDTO> find(Query query) {
@SuppressWarnings("unchecked")
List<Object[]> queryResult = query.getResultList();
List<ObjetoDTO> list = new ArrayList<>();
if (queryResult.isEmpty() == false) {
for (Object[] item : queryResult) {
ObjetoDTO dto = new ObjetoDTO();
dto.setId((Integer) item[0]);
dto.setTitulo((String) (item[1]));
dto.setDescricao((String) (item[2]));
list.add(dto);
}
}
return list;
}
what version of JPA?
– novic
Sorry, I’m using Hibernate 5.4.16.Final, I couldn’t identify which JPA version Hibernate uses :/
– Alisson Hoepers
tried it like this:
List<ObjetoDTO> list = (List<ObjetoDTO>) query.getResultList();
?– novic
In this case, I had a java.lang.Numberformatexception: For input string: "id". Because my id is an Integer.
– Alisson Hoepers
On the bench is like ? the field
id
? because the line that I passed you saw in several forum that works!– novic
You can make the query itself already return the DTO: https://stackoverflow.com/a/45934668
– hkotsubo
In the bank is as SERIAL NOT NULL id.
– Alisson Hoepers