JPA : How to convert a native query result set to POJO class collection

后端 未结 21 1979
孤街浪徒
孤街浪徒 2020-11-22 09:23

I am using JPA in my project.

I came to a query in which I need to make join operation on five tables. So I created a native query which returns five fields.

21条回答
  •  感动是毒
    2020-11-22 09:31

    Not sure if this fits here, but I had similar question and found following simple solution/example for me:

    private EntityManager entityManager;
    ...
        final String sql = " SELECT * FROM STORE "; // select from the table STORE
        final Query sqlQuery = entityManager.createNativeQuery(sql, Store.class);
    
        @SuppressWarnings("unchecked")
        List results = (List) sqlQuery.getResultList();
    

    In my case I had to use SQL parts defined in Strings somewhere else, so I could not just use NamedNativeQuery.

提交回复
热议问题