I am a student learning Spring data jpa. I am trying to solve some of practice coding questions. I have a question which i could not find answer to. I have a database table by name MYDB which has fields:
(id, firstname, lastname, rollno, major, country)
And i have an sql query like this:
select Count(*) as counts, lastname as last_name, major as major_field from MYDB group by country
The above query returns three fields: counts(which is not a db column), last_name and major_field.
I have a POJO like this:
public class MyPojo {
private int counts;
private String lastName;
private String majorField;
// Getters and Setters of all data members here
...................
}
My question is how do i map the result that i got from sql query to my POJO? I need to assign:
counts = counts(from sql query), lastName = last_name(from sql query), majorField = major_field(from sql query).
I am stuck at this point and do not know how to implement further to map result of sql query to POJO:
public interface MyRepo extends JpaRepository<MyPojo, String> {
@Query(value=MY_SQL_QUERY, nativeQuery = true)
List<MyPojo> findAll();
}
Ultimately i need to convert MyPojo to a Json object, but i know how to do that part. I am only stuck without ideas about assigning result of sql query to pojo.