在Postgres数据库中使用JPA查询

时间:2018-07-13 22:07:26

标签: postgresql spring-data-jpa

我有一个查询,需要根据用户输入按小时或天分组。我正在使用JPA查询,我想使用一个查询并按动态分组。 这是我到目前为止的内容,但无法正常工作

  

org.springframework.dao.InvalidDataAccessApiUsageException:该位置为[4]的参数不存在;默认值为0。嵌套异常是
java.lang.IllegalArgumentException:位置[4]的参数不存在

我用debug检查过,变量在那里。 这是我到目前为止的内容: Repository.java文件:

    @Query("SELECT NEW com.online.resources.Records(" +
        "COUNT(CASE WHEN (c.Agent = 'true') THEN c.customerEndpointAddress ELSE null END), " +
        "COUNT(distinct CASE WHEN c.Agent = 'true' THEN (c.customerEndpointAddress) ELSE null END), " +
        "FROM CallRecords as c " +
        "where c.connectedToSystemTimestamp>=?1 " +
        "AND c.connectedToSystemTimestamp<=?2 " +
        "GROUP BY EXTRACT(?3 FROM c.connectedToSystemTimestamp)")
List<Records> getRecords(LocalDateTime startDate, LocalDateTime endDate, String groupBy);

这是我的Resource.java文件:

    @GET
@PermitAll
@Path("Records")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response getRecords(@QueryParam("startDate") String startDate, @QueryParam("endDate") String endDate, @QueryParam("groupBy") String groupBy)  {

    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    LocalDateTime start = LocalDateTime.parse(startDate, formatter);
    LocalDateTime end = LocalDateTime.parse(endDate, formatter);
    List<Records> listOfRecords;
    try {

        listOfRecords = callRecordsRepository.getRecords(start, end, groupBy); --checking with debug parameters has the values correct at this point
        return Response.ok(listOfRecords).build();
    }
    catch (Exception ex) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

也许值得一提的是,该查询在pgAdmin中运行得很好,如果我将“ hour”替换为“ day”,它也可以正常工作:

SELECT
    COUNT(CASE WHEN (c.agent = 'true') THEN c.customer_endpoint_address ELSE null END),
    COUNT(distinct CASE WHEN c.agent = 'true' THEN (c.customer_endpoint_address) ELSE null END)
FROM public.records AS c
GROUP BY EXTRACT(day FROM c.connected_to_system_timestamp)

0 个答案:

没有答案