我在使用@SqlResultSetMapping 和@NamedNativeQuery 时遇到了一个奇怪的错误。
我的实体有:
@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 { /////// }
我的 DTO 是这样的:
public class ExampleDTO {
private Integer id;
private Integer numberOfA;
}
我的仓库:
public interface Entity extends JpaRepository<Entity,Integer> {
@Query(name = "example_query", nativeQuery = true)
List<ExampleDTO> findExampleQuery(@Param("id") Integer id);
}
在数据库上,相同的查询返回:
id | numberofa
1 11
2 5
并且映射返回一个对象:
id | numberofa
1 154
2 70
这怎么可能?