How to implement the max and min in jpa, to obtain the maximum and minimum date?

0

I have the following query

@NamedQuery(name = "findRegistroAccesoByNuserid", query = "select myRegistroAcceso from RegistroAcceso myRegistroAcceso where myRegistroAcceso.nuserid = ?1 and TO_CHAR(myRegistroAcceso.fecRegistroEntrada, 'dd/MM/yyyy') =  ?2")

ue that records back to me according to a date and an id that I pass as a parameter for filtering, that query was passed to a list

listRegistroAcceso = registroAccesoService.findRegistroAccesoByNuserid(cvePersona, strFecha);

for (RegistroAcceso registroAcceso1 : listRegistroAcceso) {

    horaEntradaBiometrico = registroAcceso1.getFecRegistroEntrada();
    System.out.println("PRUEBA ENTRADA" +registroAcceso1.getFecRegistroEntrada());


}

There is a field called fecRegistroEntrada, that I want to extract from that list and it paints me the following in console

PRUEBA ENTRADA2015-03-06 14:05:50.0
PRUEBA ENTRADA2015-03-06 14:05:54.0
PRUEBA ENTRADA2015-03-06 14:05:58.0

Now I just need to paint in a table the first record that paints that is the "PROOF ENTRADA2015-03-06 14: 05: 50.0" and the last "PROOF ENTRADA2015-03-06 14: 05: 58.0" or remove the min and the max could be said, I made a variable called Biometric input time, but when it comes to painting it in the table it takes the last value as I can do to get those two dates and be able to show them?

I did this query using max and min but it does not work and it does not raise the application, I do not know if the syntax in jpa is ok

select myRegistroAcceso from RegistroAcceso myRegistroAcceso (select min(myRegistroAcceso.fecRegistroEntrada)from RegistroAcceso myRegistroAcceso 
															where myRegistroAcceso.nuserid = ?1 and TO_CHAR(myRegistroAcceso.fecRegistroEntrada, 'dd/MM/yyyy') =  ?2
															UNION select max(myRegistroAcceso.fecRegistroEntrada) from RegistroAcceso myRegistroAcceso 
															where myRegistroAcceso.nuserid = ?1 and TO_CHAR(myRegistroAcceso.fecRegistroEntrada, 'dd/MM/yyyy') =  ?2)

So I have the service

public List<Obejct[]> findRegistroAccesoBy(Integer cveUser, String fecha);

@Override
    public List<Object[]> findRegistroAccesoBy(Integer cveUser, String fecha) {
        return new java.util.ArrayList<>(registroAccesoDAO.findRegistroAccesoBy(cveUser, fecha));
    }

and the DAO

@SuppressWarnings("unchecked")
    @Transactional
    public Set<Obect[]> findRegistroAccesoBy(Integer cveUser, String fecha) throws DataAccessException {
        Query query = createNamedQuery("findRegistroAccesoBy", -1, -1, cveUser, fecha);
        return new LinkedHashSet<Object[]>(query.getResultList());
    }

so declare the list

private List<Object[]> listFechas = new ArrayList<>();

and pass the find

listFechas = registroAccesoService.findRegistroAccesoBy(cvePersona, strFecha);

EDIT

I already got the date as follows

private List<RegistroAcceso> listRegistroAcceso2 = new ArrayList<>();
private List<Object[]> listFechas = new ArrayList<>();
private Date fechaMaxima;

//Obtiene la fecha maxima 
        listFechas = registroAccesoService.findRegistroAccesoBy(cvePersona, strFecha);

        Iterator it = listFechas.iterator();
        while (it.hasNext()) {
            Object[] line = (Object[]) it.next();
            RegistroAcceso eq = new RegistroAcceso();
            eq.setFecRegistroEntrada((Date) line[0]);

            listRegistroAcceso2.add(eq);
            fechaMaxima = listRegistroAcceso2.get(0).getFecRegistroEntrada();
            
                System.out.println("PRUEBA NUEVA LISTA" +fechaMaxima);
            }

and it paints me in console this date

TRY NEW LIST 2012-08-02 18: 55: 28.0

Now I paint it on my table, in the ENTRY field

<!--Tabla que muestra los detalles de las incidencias-->
<p:dataTable id="detalle" var="detalle" value="#{RegistroAccesoComponent.fechasIncidencias}" emptyMessage="No se encontraron registros." selectionMode="single" selection="#{RegistroAccesoComponent.fechaIncidencia}" rowKey="#{detalle.fecha}">

  <p:ajax event="rowSelect" listener="#{RegistroAccesoComponent.seleccionaFecha}" update=":form:carDetail" oncomplete="PF('modalIntentos').show();" />
  <p:column headerText="FECHA" style="width: 100px;">
    <h:outputText value="#{detalle.fecha}">
      <f:convertDateTime type="date" pattern="dd/MM/yyyy" />
    </h:outputText>
  </p:column>

  <p:column headerText="ENTRADA" style="width: 100px;">
    <h:outputText value="#{RegistroAccesoComponent.fechaMaxima}">
      <f:convertDateTime type="date" timeZone="CST" pattern="HH:mm:ss" />
    </h:outputText>

  </p:column>
  <p:column headerText="SALIDA" style="width: 100px;">
    <h:outputText>
      <f:convertDateTime type="date" timeZone="CST" pattern="HH:mm:ss" />
    </h:outputText>
  </p:column>
  <p:column headerText="INCIDENCIA">
    <h:outputText value="#{detalle.incidencia.incidencias.cveIdIncidencia} - #{detalle.incidencia.incidencias.concepto}" />
  </p:column>
  <p:column headerText="ACCIÓN" style="width: 100px;">
    <h:outputText styleClass="ui-corner-all ui-icon ui-icon-search"></h:outputText>
  </p:column>
</p:dataTable>

but he paints it in all the records, and I just want him to paint it on the date he returned me

Try this but leave null pointer

    public List<FechaIncidencia> crearFechaIncidencia(List<IncidenciaAnioMesDet> incidenciaAnioMesDet, List<RegistroAcceso> listRegistroAcceso2, int annio, int mes) {
    List<Date> fechas = crearFechas(annio, mes);

    Calendar calendar = Calendar.getInstance();
    for (Date fecha : fechas) {
        FechaIncidencia fechaIncidencia = new FechaIncidencia();
        fechaIncidencia.setFecha(fecha);
        calendar.setTime(fecha);
        for (IncidenciaAnioMesDet incidencia : incidenciaAnioMesDet) {
            if (calendar.get(Calendar.DAY_OF_MONTH) == incidencia.getDia()) {
                fechaIncidencia.setIncidencia(incidencia);

                break;
            }
            for (RegistroAcceso registroAcceso1 : listRegistroAcceso2) {
                if (calendar.getTime() == registroAcceso1.getFecRegistroEntrada()) {
                    fechaIncidencia.setRegistroAcceso(registroAcceso1);
                    break;
                }
            }
        }

        fechasIncidencias.add(fechaIncidencia);
        System.out.println("OTRA RUEBA" + fechasIncidencias.get(0).getRegistroAcceso().getFecRegistroEntrada());
    }
    return fechasIncidencias;
}
    
asked by Root93 24.03.2018 в 00:43
source

1 answer

1

It looks a bit extensive that query, would not be simpler in this way?:

select min(r.fecRegistroEntrada), max(r.fecRegistroEntrada)
from RegistroAcceso r
where r.nuserid = ?1 and 
TO_CHAR(r.fecRegistroEntrada, 'dd/MM/yyyy') = ?2

By the way, the result would be a Object[] with 2 Date 's

You would get them this way:

List<Object[]> fechas = em.createQuery(queryString, Object[].class)
    .getResultList();
    
answered by 24.03.2018 / 01:19
source