Java how to do a native query and return to a DTO list

2

I am using Spring Hibernate and am trying to do a native query with JOiN returning to a DTO list. I tried using @Query (value="", nativeQuery = true) with a List but it returns the serialized attributes. I also tried with @SqlResultSetMapping inside the DTO, but I think this would not be the most performative way.

    
asked by anonymous 27.07.2015 / 18:33

1 answer

1
  

Search for result transformer . They were made exactly to   do what you want and it works with HQL and SQL (native query)

link

If you can not, for some reason, there is another way similar to this:

List resultado = s.createSQLQuery(
  "SELECT new SeuDto(st.name, st.age) FROM Student st ";

In which you can make a new of the direct object in select and receive as an result an Object [] list that will be instances of "YourDto". This works as long as you have a constructor in "YourDto" accepting this parameters name and age , in the example case.

    
06.08.2015 / 20:02