[英]Unable to convert a @Query result to a POJO
I have a custom query on a table in Spring Boot.我在 Spring Boot 中的表上有一个自定义查询。 I would like to store the results in a POJO.我想将结果存储在 POJO 中。
public interface LegalRepository extends JpaRepository<LegalEntity, Long> {
@Query(value = "...SELECT company_id as id ,AVG((...)) as average ...." ,nativeQuery = true)
List<QueryObject> returnMeanValue();
}
LegalEntity is the main entity for data table and QueryObject is the derived one. LegalEntity是数据表的主要实体,QueryObject是派生实体。
The result of query is as follows:查询结果如下:
company_id | company_id | average平均
1 | 1 | 1560850.633333333 1560850.633333333
2 | 2 | 2365230.933333333 2365230.933333333
3 | 3 | 13714243.266666666 13714243.266666666
4 | 4 | 15375235.133333333 15375235.133333333
This is my POJO:这是我的 POJO:
@Entity
public class QueryObject {
@Id
Integer company_id;
@Column(name="average")
Double average;
public Integer getCompany_id() {
return company_id;
}
public void setCompany_id(Integer company_id) {
this.company_id = company_id;
}
public Double getAverage() {
return average;
}
public void setAverage(Double average) {
this.average = average;
}
@Override
public String toString() {
return "QueryObject [company_id=" + company_id + ", average=" + average + "]";
}
public QueryObject(Integer company_id, Double average) {
super();
this.company_id = company_id;
this.average = average;
}
}
Note that company_id is unique.请注意, company_id 是唯一的。
After executing:执行后:
@Bean
public void getUp() {
List<QueryObject> qo;
qo = leg_rep.returnMeanValue();
log.info(qo.get(0).toString());
}
The following exception is thrown:抛出以下异常:
org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type [QueryObject] for value '{1, 1560850.633333333}'; org.springframework.core.convert.ConversionFailedException:无法从类型 [java.lang.Object[]] 转换为值'{1, 1560850.633333333}'的类型 [QueryObject];
nested exception is org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [java.lang.Integer] to type [QueryObject]嵌套异常是 org.springframework.core.convert.ConverterNotFoundException:找不到能够从类型 [java.lang.Integer] 转换为类型 [QueryObject] 的转换器
How should I solve this problem?我应该如何解决这个问题?
Create an interface QueryObject
创建接口QueryObject
Create getters:创建吸气剂:
public interface QueryObject {
Integer getCompany_id();
Double getAverage();
}
Spring Data JPA will then automatically create/fill your result object. Spring 数据 JPA 将自动创建/填充您的结果 object。
Check the spring docs for details: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections查看 spring 文档了解详细信息: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#ionsproject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.