1

My Repository

 public interface ProductRepository extends JpaRepository<Product,Integer> { @Query(value = "select mp.*,u.name,p.name, p.ext_id from merchant_product mp join campaign c on c.product_id = mp.id and c.status in (4, 8)join product p on p.id = mp.product_id join user u on u.id = mp.user_id where mp.status = 4 and mp.availability = 'Y';", nativeQuery = true) List<Object> getAllProduct(); } 

This is my Query in Spring Boot i am using Spring data JPA . I need to map this to a Pojo class . So that i can use it for further processing .

can anyone help me with this.

My pojo

@Data @AllArgsConstructor public class Product { @Id //data of merchant product table //data of user table private int id; private String name; private String ext_id; } 

2 Answers 2

2
@Query(nativeQuery = true, name = "test", value = "select mp.* ...") @SqlResultSetMapping(name="test", classes = { @ConstructorResult(targetClass = Product.class, columns = {@ColumnResult(name="name"), @ColumnResult(name="id")}, @ColumnResult(name="ext_id")}) }) 

Add the SqlResultSetMapping to initialize mappings which columns from the query corresponding to the Pojo fields

Sign up to request clarification or add additional context in comments.

1 Comment

How do i add it to a method @StansislavL its says class or method expected , if i add it on top of a method
0

Id create a constructor for the params you want on Product and call -

 public interface ProductRepository extends JpaRepository<Product,Integer> { @Query(value = "select new Product(u.name,p.name, p.ext_id) from merchant_product mp join campaign c on c.product_id = mp.id and c.status in (4, 8)join product p on p.id = mp.product_id join user u on u.id = mp.user_id where mp.status = 4 and mp.availability = 'Y';", nativeQuery = true) List<Product> getAllProduct(); } 

You will need to define the params from the wildcard mp.* in your object for mapping

5 Comments

this dosent work the stack trace org.springframework.dao.InvalidDataAccessResourceUsageException: could not extract ResultSet; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarException: could not extract
Sorry _ I hadnt noticed it was a native query as i hadnt scrolled along. Can you map in your JPA model?
i didnt get you ?
Its a nativeQuery = true - so raw html - i thought it was jpa
@Rahul, I have the same use case , Are you able to find a answer to it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.