在Spring的@Query注释中无法获取数组对象响应

时间:2018-11-05 10:10:05

标签: spring-boot spring-data-jpa dto jsonresult nativequery

我的存储库

@Repository
public interface TestNativeQRepository extends CrudRepository<TestNativeQ, String> {

    @Query( value="SELECT qplt.name price_list_name,  qplab.status_code, qplab.start_date, (SELECT charge_definition_code FROM oalfsaas_repl.QP_CHARGE_DEFINITIONS_B WHERE charge_definition_id=qplab.charge_definition_id  ) chargedefinitioncode "
                + "FROM  pricelistsall qplab, PRICELISTSTL qplt "
                + " WHERE qplab.price_list_id  =qplt.price_list_id ", nativeQuery = false)
    public List<TestNativeQDTO> getAllDetails();
}

实际结果:

[{"ABC", "DEF", "15/05/2018", "XXZ"}]

预期结果

[{name: "ABC", statuscode: "DEF", startDate: "15/05/2018", chargedefintioncode: "XXZ"}]

1 个答案:

答案 0 :(得分:1)

@Nikolay在评论中给出了提示。

本机查询的结果不会自动转换为实体,您必须手动执行此操作或通过@SqlResultSetMapping和@ColumnResult定义映射。

按照以下代码进行工作。

@Entity
@Configurable
@SqlResultSetMapping(name = "someName", entities = @EntityResult(entityClass = SamplePojo.class), columns = @ColumnResult(name = "columnName"))
public class SamplePojo{
//fields and getters/setters
}

然后在查询中

List<SamplePojo> list = entityManager().createNativeQuery("Select ......", "someName").getResultList();

注意:someName在两个地方都应该相同。

引用this-question