Example usage for javax.persistence EntityManagerFactory createEntityManager

List of usage examples for javax.persistence EntityManagerFactory createEntityManager

Introduction

In this page you can find the example usage for javax.persistence EntityManagerFactory createEntityManager.

Prototype

public EntityManager createEntityManager();

Source Link

Document

Create a new application-managed EntityManager.

Usage

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard> T findSingle(Class<T> classObj, String query, Map<String, Object> map)
        throws Exception {
    T returnValue = null;//from www  .  j  av  a 2  s.  c om

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Query q = null;
    Iterator i = null;
    Map.Entry entry = null;
    try {
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        q = em.createNamedQuery(query);
        if (map != null) {
            for (i = map.entrySet().iterator(); i.hasNext();) {
                entry = (Map.Entry) i.next();
                q.setParameter((String) entry.getKey(), entry.getValue());
            }
        }
        try {
            returnValue = (T) q.getSingleResult();
        } catch (NoResultException ex) {
            returnValue = null;
        }
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
        entry = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard> List<T> findList(Class<T> classObj, String query, Map<String, Object> map,
        int firstItem, int batchSize) throws Exception {
    List<T> returnValue = null;

    EntityManagerFactory emf = null;
    EntityManager em = null;//  w ww . j a  v a 2  s . co m
    Query q = null;
    Iterator i = null;
    Map.Entry entry = null;
    try {
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        q = em.createNamedQuery(query);
        if (batchSize > 0) {
            q.setFirstResult(firstItem);
            q.setMaxResults(batchSize);
        }
        if (map != null) {
            for (i = map.entrySet().iterator(); i.hasNext();) {
                entry = (Map.Entry) i.next();
                q.setParameter((String) entry.getKey(), entry.getValue());
            }
        }
        returnValue = (List<T>) q.getResultList();
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
        entry = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard> List<StringValuesCount> getDisinctStringValuesCount(Class<T> classObj,
        String property) throws Exception {
    List<StringValuesCount> returnValue = null;

    StringBuffer hsqlQuery = null;
    EntityManagerFactory emf = null;
    EntityManager em = null;//from   w  ww .  j a  va2  s  .c o  m
    Query q = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        /* Creo la query */
        hsqlQuery = new StringBuffer();
        hsqlQuery
                .append("select o." + property + " as itemValue, count(o." + property + ") as itemCount from ");
        hsqlQuery.append(classObj.getCanonicalName());
        hsqlQuery.append(" as o");
        hsqlQuery.append(" group by o." + property + "");
        hsqlQuery.append(" order by 2 desc");
        // hsqlQuery.append("select u.publisher, count(u.publisher) from Softwares u group by u.publisher");

        q = em.createQuery(hsqlQuery.toString());

        /* Istanzio il valore di retutn */
        // List res = q.getResultList();
        returnValue = new ArrayList<StringValuesCount>();

        List<Object[]> result1 = q.getResultList();
        for (Object[] resultElement : result1) {
            String value = (String) resultElement[0];
            Long count = (Long) resultElement[1];

            StringValuesCount a = new StringValuesCount();
            a.setItemValue(value);
            a.setItemCount(count);
            returnValue.add(a);
        }

        // for (Iterator it = res.iterator(); it.hasNext();) {
        // Object obj = it.next();
        // // StringValuesCount object = (StringValuesCount)it.next();
        // // returnValue.add(object);
        // }

    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        hsqlQuery = null;
        em = null;
        q = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

/**
 * // w ww  .  j  a va2  s  . c  o m
 * Metodo che restituisce tutta la collezione di oggetti specificati come
 * parametro, tramite l'elemento di inizio e il numero di elementi
 * desiderati (mettendo a 0 questo parametro li restituisce tutti)
 * 
 * @param classObj
 * @param query
 * @param map
 * @param firstItem
 * @param batchSize
 * @return
 * @throws java.lang.Exception
 */
public <T extends EntityBaseStandard> List<T> findAll(Class<T> classObj, int firstItem, int batchSize)
        throws Exception {
    List<T> returnValue = new ArrayList<T>(); // Non piu' null cos posso
    // semplificare il codice
    // del chiamante

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzia l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        /* Crea la query */
        q = em.createQuery("from " + classObj.getName());

        /*
         * Se il numero di elementi  diverso da 0 specifico quanti e da
         * dove cominciare
         */
        if (batchSize > 0) {
            q.setFirstResult(firstItem);
            q.setMaxResults(batchSize);
        }

        /* Calcolo la collezione di elementi desiderati */
        returnValue = q.getResultList();
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

/**
 * Funziona SOLO CON IMPLEMENTAZIONE HIBERNATE
 * /* ww w .j av a 2s  .c  om*/
 * @param aList
 * @param aFilter
 * @return
 * @throws Exception
 */
public <T extends EntityBaseStandard> ArrayList<T> getFilteredList(Class<T> classObj, String aFilter)
        throws Exception {
    ArrayList<T> returnValue = null;

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Session session = null;
    Criteria cri = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        session = (Session) em.getDelegate();

        org.hibernate.Query q = session.createQuery("from " + classObj.getName());
        q.setFirstResult(0);
        q.setMaxResults(100);
        List<T> resAll = q.list();

        org.hibernate.Query filterQuery = session.createFilter(resAll, aFilter);

        /* Effettuo la query */
        returnValue = (ArrayList) filterQuery.list();
    } catch (Exception e) {
        throw e;
    } finally {
        session = null;
        cri = null;
        em = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard> List<T> listByExample(Class<T> classObj, T anExample) throws Exception {
    List<T> returnValue = null;

    EntityManagerFactory emf = null;
    EntityManager em = null;//from   w w w  .j  a v  a2  s  .c  o m
    Session session = null;
    Criteria cri = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        session = (Session) em.getDelegate();

        List res = session.createCriteria(classObj).add(Example.create(anExample).excludeZeroes().enableLike())
                .list();

        // org.hibernate.Query filterQuery = session.createFilter(resAll,
        // aFilter);

        /* Effettuo la query */
        returnValue = (List<T>) res;
    } catch (Exception e) {
        throw e;
    } finally {

        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }

        session = null;
        cri = null;
        em = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard, E extends AbstractFiltroJpa> int findFilterCount(Class<T> classObj,
        E filtro) throws Exception {
    int returnValue = 0;

    EntityManagerFactory emf = null;
    EntityManager em = null;//from   w  w w.  ja va2  s. com
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzia l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        StringBuilder hqlQuery = new StringBuilder();

        hqlQuery.append("select count(t) from " + classObj.getName() + " t ");

        hqlQuery.append(" WHERE ");
        if (filtro != null && StringUtils.isNotEmpty(filtro.getSQLWhere())) {
            hqlQuery.append(filtro.getSQLWhere());
        }

        if (filtro != null) {
            hqlQuery.append(filtro.getSQLSort());
        }

        /* Crea la query */
        logger.debug(hqlQuery.toString());
        q = em.createQuery(hqlQuery.toString());

        /*
         * Se il numero di elementi  diverso da 0 specifico quanti e da
         * dove cominciare
         */
        // if (filtro != null && filtro.getRighePerPagina() > 0) {
        // q.setFirstResult(filtro.getPagina() *
        // filtro.getRighePerPagina());
        // q.setMaxResults(filtro.getRighePerPagina());
        // }

        if (filtro != null && filtro.getListaParametri() != null) {
            for (Entry<String, Object> parametro : filtro.getListaParametri().entrySet()) {
                q.setParameter(parametro.getKey(), parametro.getValue());
            }
        }

        /* Calcolo la collezione di elementi desiderati */
        returnValue = Integer.parseInt(q.getSingleResult().toString());

    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

public <T extends EntityBaseStandard, E extends AbstractFiltroJpa> List<T> findFilter(Class<T> classObj,
        E filtro) throws Exception {
    List<T> returnValue = null;

    EntityManagerFactory emf = null;
    EntityManager em = null;//from  w w w. jav  a  2 s.  c  om
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzia l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        StringBuilder hqlQuery = new StringBuilder();

        hqlQuery.append("select t from " + classObj.getName() + " t ");

        hqlQuery.append(" WHERE ");
        if (filtro != null && StringUtils.isNotEmpty(filtro.getSQLWhere())) {
            hqlQuery.append(filtro.getSQLWhere());
        }

        if (filtro != null) {
            hqlQuery.append(filtro.getSQLSort());
        }

        /* Crea la query */
        logger.debug(hqlQuery.toString());
        q = em.createQuery(hqlQuery.toString());

        /*
         * Se il numero di elementi e' diverso da 0 specifico quanti e da
         * dove cominciare
         */
        if (filtro != null && filtro.getRighePerPagina() > 0) {
            q.setMaxResults(filtro.getRighePerPagina());
            if (filtro.getPagina() > 0) {
                q.setFirstResult((filtro.getPagina() - 1) * filtro.getRighePerPagina());

            }
        }

        if (filtro != null && filtro.getListaParametri() != null) {
            for (Entry<String, Object> parametro : filtro.getListaParametri().entrySet()) {
                q.setParameter(parametro.getKey(), parametro.getValue());
            }
        }

        /* Calcolo la collezione di elementi desiderati */
        returnValue = q.getResultList();

    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

/**
 * /*from   w  w  w  . j  a v a 2 s  .com*/
 * Metodo che restituisce il numero di elementi dato un certo tipo, la query
 * e la mappa dei parametri
 * 
 * @param classObj
 * @return
 * @throws java.lang.Exception
 */
public int getItemCount2(Class<? extends EntityBaseStandard> classObj, String query, Map<String, Object> map)
        throws Exception {
    int returnValue = 0;

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Map.Entry entry = null;
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        /* Creo la query */
        q = em.createNamedQuery(query);

        /* Verifico la validit della mappa dei parametri */
        if (map != null) {

            /* Per ogni elemento della mappo setto un parametro */
            for (i = map.entrySet().iterator(); i.hasNext();) {
                entry = (Map.Entry) i.next();
                q.setParameter((String) entry.getKey(), entry.getValue());
            }
        }

        /* Calcolo il valore di ritorno */
        returnValue = ((Long) q.getSingleResult()).intValue();
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        entry = null;
        em = null;
        q = null;
        i = null;
    }
    return returnValue;
}

From source file:it.webappcommon.lib.jpa.ControllerStandard.java

/**
 * /*  w ww.  j a  v a 2  s . c om*/
 * Metodo che restituisce il numero di elementi dato un certo tipo, la query
 * e la mappa dei parametri
 * 
 * @param classObj
 * @return
 * @throws java.lang.Exception
 */
public int getItemCountCustomQuery(Class<? extends EntityBaseStandard> classObj, String query,
        Map<String, Object> map) throws Exception {
    int returnValue = 0;

    EntityManagerFactory emf = null;
    EntityManager em = null;
    Map.Entry entry = null;
    Iterator i = null;
    Query q = null;
    try {
        /* Istanzio l'entity manager */
        emf = getEntityManagerFactory();
        em = emf.createEntityManager();

        /* Creo la query */
        q = em.createQuery(query);

        /* Verifico la validit della mappa dei parametri */
        if (map != null) {

            /* Per ogni elemento della mappo setto un parametro */
            for (i = map.entrySet().iterator(); i.hasNext();) {
                entry = (Map.Entry) i.next();
                q.setParameter((String) entry.getKey(), entry.getValue());
            }
        }

        /* Calcolo il valore di ritorno */
        returnValue = ((Long) q.getSingleResult()).intValue();
    } catch (Exception e) {
        throw e;
    } finally {
        if (!passedEm) {
            PersistenceManagerUtil.close(em);
        }
        em = null;
        q = null;
        i = null;
        entry = null;
    }
    return returnValue;
}