createNativeQuery - 返回页面

时间:2017-12-15 10:03:05

标签: spring-data-jpa

我正在看例子,但我没有看到具体的事情。我有我的存储库的以下自定义实现。

我需要而不是返回List<ImputacionData>我返回了Page<ImputacionData>,但我不知道是否可能,以及如何做到。

我看到了文档和示例,但我仍然没有看清楚。 有人可以帮助我

public final EntityManager entityManager;

public ImputacionRepositoryImpl(EntityManager entityManager) {
    this.entityManager = entityManager;
}

@Override
public List<ImputacionData> imputacionesList() {
    Query q = entityManager.createNativeQuery("SELECT " + "empleado.id as empleadoId,"
            + "MAX(imputacion.dia) as dia," + "imputacion.id as id " + "FROM rrhh.empleado empleado "
            + "JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id "
            + "WHERE empleado.id NOT IN (SELECT empleado.id " + "FROM rrhh.empleado empleado "
            + "LEFT JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id " + "WHERE "
            + "(imputacion.dia >= '2017-06-01' AND imputacion.dia <= '2017-10-31') "
            + "GROUP BY empleado.id,imputacion.empleado, imputacion.id) " + "AND true=true "
            + "GROUP BY empleado.id,imputacion.empleado, imputacion.id ORDER BY imputacion.dia DESC");
    List<ImputacionData> imputaciones = q.getResultList();
    return imputaciones;
}

1 个答案:

答案 0 :(得分:3)

所以你可以使用page implementation in Spring,在你的情况下,它可能看起来像:

    @Override
    public Page<ImputacionData> imputacionesList() {
    Query q = entityManager.createNativeQuery("SELECT " + "empleado.id as empleadoId,"
            + "MAX(imputacion.dia) as dia," + "imputacion.id as id " + "FROM rrhh.empleado empleado "
            + "JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id "
            + "WHERE empleado.id NOT IN (SELECT empleado.id " + "FROM rrhh.empleado empleado "
            + "LEFT JOIN rrhh.imputacion imputacion ON imputacion.empleado = empleado.id " + "WHERE "
            + "(imputacion.dia >= '2017-06-01' AND imputacion.dia <= '2017-10-31') "
            + "GROUP BY empleado.id,imputacion.empleado, imputacion.id) " + "AND true=true "
            + "GROUP BY empleado.id,imputacion.empleado, imputacion.id ORDER BY imputacion.dia DESC");
    Page<ImputacionData> page = new PageImpl<>(q.getResultList());
    return page;
}