At the time of executing a NamedQuery it returns data type object [] instead of Java Class type

0

I have a NamedQuery that only brings certain columns, the problem is that in my method

public List<T> NewEncontarSqlArmado(Map parametros, Asignaturas asignatura) throws ConsultarException {
    try {
        Iterator it = parametros.entrySet().iterator();
        String consulta = "";
        String orden = "";
        boolean all = false;
        int maxResults = -1;
        int firstResult = -1;
        String tabla = entityClass.getSimpleName();
        Map par = new HashMap();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            String clave = (String) e.getKey();
            if (clave.contains(";consulta")) {
                consulta = (String) e.getValue();
            } else if (clave.contains(";inicial")) {
                firstResult = (Integer) e.getValue();
                all = true;
            } else if (clave.contains(";orden")) {
                orden = " order by " + (String) e.getValue();

            } else if (clave.contains(";final")) {
                all = true;
                maxResults = (Integer) e.getValue();
            } else {
                par.put(clave, e.getValue());
            }
        }

        javax.persistence.Query q = getEntityManager().createNamedQuery(consulta).setParameter("asignatura", asignatura);

        it = par.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            String clave = (String) e.getKey();

            q.setParameter(clave, e.getValue());
        }
        if (all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        return q.getResultList();
    } catch (Exception e) {
        throw new ConsultarException(entityClass.toString(), e);
    }
}  

I return everything in Object [] type instead of the class I'm using at that moment which would be "File" and when I take it on my list in the file bean, I get the error that it could not take the object type value in List because it needs to be type "File".

I hope it was clear with my details: / I appreciate your help in advance.

This is the named Query that I have

@NamedQuery(name = "Archivos.SinArchivo", query = "SELECT a.id, a.codigo, a.nombre, a.descripcion, a.tipo, a.asignatura, a.activo, a.userid, a.fecha, a.tipoarchivo, a.demo, a.filepath FROM Archivos a WHERE a.asignatura= :asignatura and a.activo=true")
    
asked by Pedro Acosta Pilataxi 29.09.2017 в 18:05
source

3 answers

1
  

@NamedQuery (name="Files.SinFile", query="SELECT a.id, a.codigo, a.name, a.description, a.type, a.asignature, a.active, a.userid, a.date, a.typefile, a.demo, a.filepath FROM Files to WHERE a.asignature =: subject and a.activo = true ")

The problem is that you are calling different attributes of the object (class) separately ie you are calling a.id, a.codigo, a.nombre, when you could call a in that case if you generate the result as an object if you cast

 return (List<Archivo>) q.getResultList();

If you need to bring those specific objects considering that they are not all the proper ones of the class , then the array of objects will always come back to you since all these are data > individuals in that case you do not have another if you do not go through that Object[] with a for and take them one by one add them to an instance of File with the methods set() , get()

I reiterate that if these data that you call in the query are all those that belong to Archivos then in the query you could only return

@NamedQuery(name = "Archivos.SinArchivo", query = "SELECT a FROM Archivos a WHERE a.asignatura= :asignatura and a.activo=true")
    
answered by 29.09.2017 / 18:57
source
0

Your problem is that you use createNamedQuery(consulta) , which returns a Query object, but you should use:

TypedQuery<Archivo> query= createNamedQuery(consulta,Archivo.class);
...
return query.getResultList();

That does return a List<Archivo>

    
answered by 29.09.2017 в 18:22
0

Is that you are not specifying the type of output object in your NamedQuery , then it should look like this

        List<Archivos> archivosList= null;

        Query query = em.createNamedQuery("Archivos.SinArchivo");
        query.setParameter("asignatura", asignatura);
        archivosList= query.getResultList();

Also your query can be left like this, you only refer to the same table:

@NamedQuery(name = "Archivos.SinArchivo", query = "SELECT a FROM Archivos a WHERE a.asignatura= :asignatura AND a.activo=true")

You could try the following example as is and verify that it recovers the information without problem:

    @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
    public List<Archivos> NewEncontarSqlArmado(Asignaturas asignatura) {
        List<Archivos> archivosList= null;

        Query query = getEntityManager().createNamedQuery("Archivos.SinArchivo");
        query.setParameter("asignatura", asignatura);
        archivosList= query.getResultList();

        return archivosList;
    }

Or in your example it would look like this:

public List<T> NewEncontarSqlArmado(Map parametros, Asignaturas asignatura) throws ConsultarException {
    try {
        Iterator it = parametros.entrySet().iterator();
        String consulta = "";
        String orden = "";
        boolean all = false;
        int maxResults = -1;
        int firstResult = -1;
        String tabla = entityClass.getSimpleName();
        Map par = new HashMap();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            String clave = (String) e.getKey();
            if (clave.contains(";consulta")) {
                consulta = (String) e.getValue();
            } else if (clave.contains(";inicial")) {
                firstResult = (Integer) e.getValue();
                all = true;
            } else if (clave.contains(";orden")) {
                orden = " order by " + (String) e.getValue();

            } else if (clave.contains(";final")) {
                all = true;
                maxResults = (Integer) e.getValue();
            } else {
                par.put(clave, e.getValue());
            }
        }

        javax.persistence.Query q = getEntityManager().createNamedQuery(consulta).setParameter("asignatura", asignatura);

        it = par.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry e = (Map.Entry) it.next();
            String clave = (String) e.getKey();

            q.setParameter(clave, e.getValue());
        }
        if (all) {
            q.setMaxResults(maxResults);
            q.setFirstResult(firstResult);
        }
        List<Archivos> archivosList= q.getResultList()
        return archivosList;
    } catch (Exception e) {
        throw new ConsultarException(entityClass.toString(), e);
    }
} 
    
answered by 29.09.2017 в 19:04