How to Map a JPA create native query to projections

Tags: , , ,

I am trying to get count from a postgreSQL database using Spring Data JPA createNativeQuery. However, the query is returning null instead of the actual values.

Here is the JPA createNativeQuery statement below:

Query q = entityManager.createNativeQuery("SELECT null AS id, 
count(case when (IS_QUERIED = false AND SUBMITTED = true) 
then 1 else null end) AS pending,  
count(case when ( (SUBMITTED = true)) then 1 else null end) AS submitted, 
count(*) AS totalApplications FROM ANNUAL_RETURNS ", AnnualReturn.class);
//Note: AnnualReturn is the name of the @Entity class

List <AnnualReturn> countList=q.getResultList();

return countList;

I need help mapping the “submitted”, “pending” and “totalApplications” instances from my query in a way that returns the result as below.

Result expected is:

"data": {
        "totalApplications": 2000,
        "submitted": 560,
        "pending": 60,

Result am getting:

    "data": [

I would appreciate any help.


I do not know why but I believe SELECT null AS id is causing null record to be fetched.

If you don’t want id to be fetched then you can use projection with custom RowMapper or DTO projection.

See below:

class A {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Long id;
    String name;
    int age;

    public A() {
    public A(String name, int age) { = name;
        this.age = age;

interface ARepo extends JpaRepositoryImplementation<A, Long> {

class Init {
    final ARepo repo;
    final EntityManager em;

    public void init(ContextRefreshedEvent evt) { A("XX", 5)); A("ZZ", 6)); A("AA", 11)); A("AB", 12));

        Query q = em.createNativeQuery("select 0 as id, as name, a.age as age  from A a where > 10 ", A.class);

        System.out.println(q.getResultList()); //fetches AA and BB 
        //if you change the query to select null as id  it would return [ null, null]

Source: stackoverflow