Rachit Agrawal
Rachit Agrawal

Reputation: 735

Getting List of POJOs after executing a native query

I am working with Stored Procedures and Views. I call these using entityManager.createNativeQuery . The problem is that createNativeQuery method returns a List<Object[]> but I would rather like to get List<PojoName>. How to achieve this. I am using Hibernate, Spring Data Jpa.

I have heard SqlResultsetMapping annotation can achieve this, but I have not found any example to this.

Please help

Upvotes: 2

Views: 766

Answers (2)

FvZ
FvZ

Reputation: 45

I'm facing a similar problem withnative query with multiple joins and found this ticket DATAJPA-223. Looks like you can't return a type that's not a entity if it's a native query, even using SqlResultsetMapping.

Upvotes: 0

Jakub Kubrynski
Jakub Kubrynski

Reputation: 14149

Have you tried using @Query annotation instead of createNativeQuery method? You can annotate method in your repository and use native query:

@Query(value = "SELECT * FROM pojo_name p WHERE p.id between ?1 and ?2", nativeQuery = true)
List<PojoName> findByPojos(Long from, Long to);

Upvotes: 1

Related Questions