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

后端 未结 21 1774
孤街浪徒
孤街浪徒 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:38

    I tried a lot of things as mentioned in the above answers. The SQLmapper was very confusing as to where to put it. Non managed POJOs only were a problem. I was trying various ways and one easy way I got it worked was as usual. I am using hibernate-jpa-2.1.

    List testInfoList = factory.createNativeQuery(QueryConstants.RUNNING_TEST_INFO_QUERY)
                        .getResultList();
    

    The only thing to take care was that POJO has same member variable names as that of the query ( all in lowercase). And apparently I didn't even need to tell the target class along with query as we do with TypedQueries in JPQL.

    TestInfo.class

    @Setter
    @Getter
    @NoArgsConstructor
    @ToString
    public class TestInfo {
    
        private String emailid;
        private Long testcount;
    
        public TestInfo(String emailId, Long testCount) {
            super();
            this.emailid = emailId;
            this.testcount = testCount;
        }
    
    }
    

提交回复
热议问题