how to get the maximum and minimum date of a query in jpa with java?

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")

that returns records according to a date and an id that happened 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 on a table the first record that paints which is the "PROOF ENTRADA2015-03-06 14: 05: 50.0" and the last "PROOF ENTRADA2015-03-06 14: 05: 58.0" that is to say to extract the min and the max one could say, I made a variable called Entering HourBiometrics, but when it comes to painting it in the table it takes the last value How can I get those two dates and be able to show them?

    
asked by Root93 21.03.2018 в 17:03
source

1 answer

0

If your application does not require a lot of performance you could use a union and only change the query in the following way:

@NamedQuery(name = "findRegistroAccesoByNuserid", query = "select * from (select MIN(myRegistroAcceso) fecha from RegistroAcceso myRegistroAcceso where myRegistroAcceso.nuserid = ?1 and TO_CHAR(myRegistroAcceso.fecRegistroEntrada, 'dd/MM/yyyy') =  ?2 UNION select MAX(myRegistroAcceso)fecha from RegistroAcceso myRegistroAcceso where myRegistroAcceso.nuserid = ?1 and TO_CHAR(myRegistroAcceso.fecRegistroEntrada, 'dd/MM/yyyy') =  ?2) where fecha is not null" )

So you would get the minimum date and the maximum date in a single query.

    
answered by 21.03.2018 в 18:19