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.