how can a jpa script create a native query for projections?

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]

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top