Select query with SUM java

0

I need to make an inquiry by counting the number of hours in a field, this is my code.

the field is an integer, since only a number of 2,3,4 hours is saved, it is not required to be a time type.

public Object findBySum(Integer iduser){
    Query q =  getEntityManager().createNativeQuery("select SUM(a.cantidadhoras) FROM Asistenciacapacitaciones a JOIN Empleados e on e.iduser = a.iduser WHERE e.iduser =   "+ iduser, Asistenciacapacitaciones.class)
            .setParameter("iduser", iduser);
            List count = (List) q.getResultList();
    return ((BigInteger)count.get(0)).longValue();

}

in doing so, it returns the following error,

javax.persistence.PersistenceException: Exception [EclipseLink-6044] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: The primary key read from the row [ArrayRecord(
     => 23)] during the execution of the query was detected to be null.  Primary keys must not contain null.
Query: ReadAllQuery(referenceClass=Asistenciacapacitaciones sql="select SUM(a.cantidadhoras) FROM Asistenciacapacitaciones a JOIN Empleados e on e.iduser = a.iduser WHERE e.iduser =   1")

so I see if it brings me the result of the query that is 23, and I already tested it in the persistence unit, the query is fine.

What am I doing wrong? Or what am I missing?

Thanks in advance.

    
asked by BastianBurst 26.09.2017 в 22:12
source

2 answers

0

Ready, in the end the solution was to cast it as an object and pass it to a rest method that will show it as a json.

public Object findBySum(Integer iduser) {
    Query q = getEntityManager().createNativeQuery("select SUM(a.cantidadhoras) FROM asistenciacapacitaciones a JOIN empleados e on e.iduser = a.iduser WHERE e.iduser =   " + iduser)
            .setParameter("iduser", iduser);
    Object res = (Object) q.getSingleResult();
    return res;

}

The json that returns has this look

{"type":"decimal","value":5}

Already in the front was only work with that json object, which is already easy to do.

Thanks for responding equally.

    
answered by 27.09.2017 / 22:15
source
0

EclipseLink is case sensitive in less recent versions by default unless the eclipselink.jpa.uppercase-column-names persistent property is set to true.

This may be the problem with native queries if the database returns the name of the column in upper case, for example: "ID" when it is defined as a lowercase "id".

Try to change the column definitions in the annotations to match those used by your database or add the property with a value of true, check that your columns have the same name as in the database.

Also so that you use the .setParameter("iduser", iduser); if you are concatenating it in case it should be like this:

public Object findBySum(Integer iduser){
    Query q =  getEntityManager().createNativeQuery("select SUM(a.cantidadhoras) FROM Asistenciacapacitaciones a JOIN Empleados e on e.iduser = a.iduser WHERE e.iduser =   "+ iduser, Asistenciacapacitaciones.class);
            List count = (List) q.getResultList();
    return ((BigInteger)count.get(0)).longValue();

}

or

public Object findBySum(Integer iduser){
    Query q =  getEntityManager().createNativeQuery("select SUM(a.cantidadhoras) FROM Asistenciacapacitaciones a JOIN Empleados e on e.iduser = a.iduser WHERE e.iduser =   :iduser", Asistenciacapacitaciones.class)
            .setParameter("iduser", iduser);
            List count = (List) q.getResultList();
    return ((BigInteger)count.get(0)).longValue();

}

Also when The return type is used when you want the JPA provider to build an entity from the results, but in this case you want the raw data.

public Object findBySum(Integer iduser){
        Query q =  getEntityManager().createNativeQuery("select SUM(a.cantidadhoras) FROM Asistenciacapacitaciones a JOIN Empleados e on e.iduser = a.iduser WHERE e.iduser =   :iduser")
                .setParameter("iduser", iduser);
                List count = (List) q.getResultList();
        return ((BigInteger)count.get(0)).longValue();

    }

Or another way it could be:

BigDecimal sum =(BigDecimal)em.createNativeQuery ("select SUM(a.cantidadhoras) FROM Asistenciacapacitaciones a JOIN Empleados e on e.iduser = a.iduser WHERE e.iduser =  ?")
.setParameter(1, iduser)
.getSingleResult();
    
answered by 26.09.2017 в 23:11