I'm having a strange error using @SqlResultSetMapping and @NamedNativeQuery.
My Entity has:
@NamedNativeQuery(
name = "example_query",
query = "SELECT table_A.id, " +
"COUNT(*) AS numberOfA " +
"FROM table_A table_A " +
" JOIN example example ON example.id = :idExample " +
"GROUP BY id " +
"ORDER BY numberOfA DESC;",
resultSetMapping = "myMapping"
)
@SqlResultSetMapping(
name = "myMapping",
classes = @ConstructorResult(
targetClass = ExampleDTO.class,
columns = {
@ColumnResult(name = "id", type = Integer.class),
@ColumnResult(name = "numberOfA", type = Integer.class)
}
)
)
public class Entity implements Serializable { /////// }
My DTO is like:
public class ExampleDTO {
private Integer id;
private Integer numberOfA;
}
My Repository:
public interface Entity extends JpaRepository<Entity,Integer> {
@Query(name = "example_query", nativeQuery = true)
List<ExampleDTO> findExampleQuery(@Param("id") Integer id);
}
On the database the same query return:
id | numberofa
1 11
2 5
and the mapping return an object with:
id | numberofa
1 154
2 70
How is that possible?