JPA execute List Result Query - Java javax.persistence

Java examples for javax.persistence:EntityManager

Description

JPA execute List Result Query

Demo Code


import java.util.List;
import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

public class Main{
    @SuppressWarnings("rawtypes")
    public static List executeListResultQuery(EntityManager em, String query) {
        Query consulta = em.createQuery(query);
        List resultado = consulta.getResultList();
        return resultado;
    }//from  w  w w .  j  a  v  a2 s  .  co m
    @SuppressWarnings("rawtypes")
    public static List executeListResultQuery(EntityManager em,
            String query, Object... params) {
        Query consulta = em.createQuery(query);
        int paramNumber = 0;
        for (Object param : params) {
            consulta.setParameter(paramNumber++, param);
        }
        List resultado = consulta.getResultList();
        return resultado;
    }
    @SuppressWarnings("rawtypes")
    public static List executeListResultQuery(EntityManager em,
            String query, Map<String, Object> params) {
        Query consulta = em.createNamedQuery(query);
        for (String paramName : params.keySet()) {
            consulta.setParameter(paramName, params.get(paramName));
        }
        List resultado = consulta.getResultList();
        return resultado;
    }
}

Related Tutorials