我如何忽略:PSQLException:在此ResultSet中找不到列名clothStyle

时间:2018-07-24 04:39:35

标签: jpa spring-data-jpa

我创建了一个查询,该查询仅从表中的行中获取4个项目,而该表中不包含列布料样式,因此我理解为什么会收到错误,但是如何告诉Spring Jpa或JPA是故意的。我只想要ID,名称和颜色表?

这是我的代码:

@RequestMapping(value = "/query/material",method = RequestMethod.GET)

public String QueryMaterialTable(HttpServletRequest request){

    DataTableRequest<Material> dataTableInRQ = new DataTableRequest<Material>(request);
    PaginationCriteria pagination = dataTableInRQ.getPaginationRequest();

    String baseQuery = "SELECT id as id, time as time, name as name, color as color, price as price, (SELECT COUNT(1) FROM MATERIAL) AS totalrecords  FROM MATERIAL";
    String paginatedQuery = AppUtil.buildPaginatedQuery(baseQuery, pagination);

    System.out.println(paginatedQuery);

    Query query = entityManager.createNativeQuery(paginatedQuery, Material.class);

    @SuppressWarnings("unchecked")
    List<Material> materialList = query.getResultList();

    DataTableResults<Material> dataTableResult = new DataTableResults<Material>();
    dataTableResult.setDraw(dataTableInRQ.getDraw());
    dataTableResult.setListOfDataObjects(materialList);
    if (!AppUtil.isObjectEmpty(materialList)) {
        dataTableResult.setRecordsTotal(String.valueOf(materialList.size())
        );
        if (dataTableInRQ.getPaginationRequest().isFilterByEmpty()) {
            dataTableResult.setRecordsFiltered(String.valueOf(materialList.size()));
        } else {
            dataTableResult.setRecordsFiltered(String.valueOf(materialList.size()));
        }
    }
    return new Gson().toJson(dataTableResult);
}

1 个答案:

答案 0 :(得分:1)

如果我的问题正确,那么您的问题在于以下两行:

Query query = entityManager.createNativeQuery(paginatedQuery, Material.class);
List<Material> materialList = query.getResultList();

您可以通过多种方法来解决此问题: