Home » Java » Mapping of Stored Procedure resultset to different/embedded Java POJO in Spring JPA

Mapping of Stored Procedure resultset to different/embedded Java POJO in Spring JPA

Posted by: admin September 26, 2018 Leave a comment

Questions:

I want to understand if we can directly map our stored procedure resultset to nested java POJOs directly.

I am having following POJO structure:

public class Person {
     private PersonInformation pInfo;
     private PersonContactInformation pContactInfo;
     //getters and setters..

}

public class PersonInformation {
   private String firstName;
   private String lastName;
   //getters and setters...
}

public class PersonContactInformation {
  private String contactNumber;
  private String email;
  //getters and setters
}

Query in Stored procedure is like this which results multiple person data:

 select p.fName as firstName, p.lName as lastName, p.cNumber as contactNumber, p.email as email from person p where p.id in (1,2,3,4,5);

So, I am calling the stored procedure from Spring JPA like this:

StoredProcedureQuery uspGetEnrichmentPersonInfo = entityManager.createStoredProcedureQuery("Get_Person_Info");

List<Object[]> resultList = uspGetEnrichmentPersonInfo.getResultList();

I want to know how can I directly populate all the POJOs ? Like for example,

List<Person> resultList = ( List<Person>) uspGetEnrichmentPersonInfo.getResultList();
Answers: