Jpa Data Repositories With Sqlresultsetmapping And Native Queries
I was stuck with the following situation: My entities are related to each other, but in such a way that i could not use JPQL. I was forced to use native SQL. Now I want to map thes
Solution 1:
Add the missing resultClass
@NamedNativeQuery(name = "findAllDataMapping", resultClass = Entity.class, query="sql")Or
@NamedNativeQuery(name = "findAllDataMapping", resultClass = MyVO.class, resultSetMapping ="findAllDataMapping" query = "sql")and lastly call the query in your repository
@Query(nativeQuery = true, name = "findAllDataMapping")
List<MyVO> findAllOfMyVO(@Param("param1") String param1, @Param("param2") String param2);
Solution 2:
You are almost there, but for the below parts
- The whole
@SqlResultSetMappingand@NamedNativeQueryhas to be present in the Entity and not the value Object. In your case it should be in the MyEntity class and **not ** the MyVO class. This should resolve your exception. That will still not do. After you do the above, change the below
@NamedNativeQuery(name = "findAllDataMapping", to @NamedNativeQuery(name = "MyEntity.findAllDataMapping",
Finally, in some cases you need to be explicit in your definition of @ColumnResult(name = "userFirstName"). If it is a complex field like ZonedDateTime or Boolean you may have to explicity state @ColumnResult(name = "date_created", type = ZonedDateTime.class).
Hope that helps.
Solution 3:
You need to mark your query as a query :) And you need to use MyVO instead of MyEntity, because that is the entity you have your resulsts mapped to
@Repository
public interface MyRepository extends JpaRepository<MyVO, Long> {
@Query(nativeQuery = true)
List<MyVO> findAllOfMyVO(@Param("param1") String param1, @Param("param2") String param2);
}
Post a Comment for "Jpa Data Repositories With Sqlresultsetmapping And Native Queries"