Mapping native query results to POJO


Andy
 
Edited

Down to interpretation whether that is part of the JPA spec or not (the JPA spec is inexplicit as ever), but we consider it to be.
In the simplest case—i.e., when the results of the query are limited to entities of a single entity class andthe mapping information can be derived from the columns of the SQL result and the object/relational mapping metadata—it is sufficient to specify only the expected class of the entity result.
The JDO API defines what we support as required handling, so you get it with JPA for free.
You can either have a constructor with arguments the same in type as the result columns, or a default constructor plus setters for properties with those column names.


@Mogaba
 

Hello,

I found out that I can map native query results to POJOs without any additional configuration:

public class Person {
	private int id;
	private String name;
	
	// getters/setters
}

List<Person> persons = em.createNativeQuery("SELECT id, name FROM myschema.persons", Person.class).getResultList();

Is this a DataNucleus feature? As far as I understand, JPA requires you to define @SqlResultSetMapping to be able to map to POJOs.