Spring JPA -> 将结果集映射到不存在的实体



我需要将本机查询的返回映射到对象

  • 这是我的本机查询
@Query(value = "select collector from relation;", nativeQuery = true)
Stream<RelationStatistics> findRelationsStatistics();
  • 这是我的对象
public class RelationStatistics {
private String collector;
public RelationStatistics(String collector) {
this.collector = collector;
}
public String getCollector() {
return collector;
}
public void setCollector(String collector) {
this.collector = collector;
}
}
  • 这是我的测试
@Test
public void test() {
Stream<RelationStatistics> test = relations.findRelationsStatistics();
test.forEach(item -> System.out.println(item));
}

这个测试返回我:

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [RelationStatistics]

这是一个只有一个字符串属性的示例,但原始本机查询是一个很大的请求,因此创建实体将太困难。

我找到了SqlResultSetMapping但我真的不明白如何正确使用它

如果有人知道可以做什么0_o

我在这里找到了一个解决方案:JPA:如何将本机查询结果集转换为POJO类集合

使用带有接口的投影作为对象使用 getCollector(( 方法并使用此

映射

最新更新