1

I have this query written in Spring Data JPA :

@Query(value = "SELECT x,y, sum(x,y) FROM table WHERE x = ?1 groupby x,y ", nativeQuery = true) 

and I have entity class as

@Entity class abc { int x; int y; //setter and getter } 

How to map the result of this query to a POJO or Entity class??

PS: This works:

@Query(value = "SELECT * FROM table WHERE x = ?1 ", nativeQuery = true) 
1
  • What exactly is the problem? Are you getting an exception? The wrong result? Commented Apr 19, 2015 at 7:10

1 Answer 1

1

You have to map the result of the query to a POJO created, by passing it .class into the query. This can be done by calling createNativeQuery() on entity manager. e.g., List<abc> myList = (List<abc>)em.createNativeQuery("your query here", abc.class). abc.java should have following two things

    1. attribute names matching to the column names in the select clause of the query and
    2. a default constructor
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.