Example usage for javax.persistence EntityManager createNamedQuery

List of usage examples for javax.persistence EntityManager createNamedQuery

Introduction

In this page you can find the example usage for javax.persistence EntityManager createNamedQuery.

Prototype

public Query createNamedQuery(String name);

Source Link

Document

Create an instance of Query for executing a named query (in the Java Persistence query language or in native SQL).

Usage

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

private int getTotal(String type) {
    EntityManager em = null;
    try {/*from   w ww  .j a v a  2  s .c  om*/
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findTotalByType");
        q.setParameter("type", type);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

private int getTotal(String type, String mediapackageId) {
    EntityManager em = null;
    try {//from  w w w.  j a v  a2s.  c  om
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findTotalByTypeAndMediapackageId");
        q.setParameter("type", type);
        q.setParameter("mediapackageId", mediapackageId);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

private int getTotal(Calendar calBegin, Calendar calEnd) {
    EntityManager em = null;
    try {//from  w  w  w.  j  ava2 s .  c  o  m
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findTotalByIntervall");
        q.setParameter("begin", calBegin, TemporalType.TIMESTAMP);
        q.setParameter("end", calEnd, TemporalType.TIMESTAMP);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

private int getTotal(String type, Calendar calBegin, Calendar calEnd) {
    EntityManager em = null;
    try {/*from   ww w .j a v  a  2  s  .  c  o m*/
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findTotalByTypeAndIntervall");
        q.setParameter("type", type);
        q.setParameter("begin", calBegin, TemporalType.TIMESTAMP);
        q.setParameter("end", calEnd, TemporalType.TIMESTAMP);
        return ((Long) q.getSingleResult()).intValue();
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opencastproject.search.impl.persistence.SearchServiceDatabaseImpl.java

private void populateSeriesData() throws SearchServiceDatabaseException {
    EntityManager em = null;
    EntityTransaction tx = null;/*www  .j  a v a 2 s.c o m*/
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        TypedQuery<SearchEntity> q = (TypedQuery<SearchEntity>) em.createNamedQuery("Search.getNoSeries");
        List<SearchEntity> seriesList = q.getResultList();
        for (SearchEntity series : seriesList) {
            String mpSeriesId = MediaPackageParser.getFromXml(series.getMediaPackageXML()).getSeries();
            if (StringUtils.isNotBlank(mpSeriesId) && !mpSeriesId.equals(series.getSeriesId())) {
                logger.info("Fixing missing series ID for episode {}, series is {}", series.getMediaPackageId(),
                        mpSeriesId);
                series.setSeriesId(mpSeriesId);
                em.merge(series);
            }
        }
        tx.commit();
    } catch (Exception e) {
        logger.error("Could not update media package: {}", e.getMessage());
        if (tx.isActive()) {
            tx.rollback();
        }
        throw new SearchServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

@Override
public Object getScalarResult(String namedQuery, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {/*from   w  ww  .jav a  2s.com*/
        Query query = em.createNamedQuery(namedQuery);
        JpaUtil.bindQueryParams(query, posParams);
        return query.getSingleResult();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

@Override
public Number count(String namedQuery) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {/* w  w  w .ja va2  s .c om*/
        return (Number) em.createNamedQuery(namedQuery).getSingleResult();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}/*from www  .j  a va2s . co m*/
 */
@SuppressWarnings("unchecked")
@Override
public <T> List<T> findByNamedQuery(String namedQuery, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        Query query = em.createNamedQuery(namedQuery);
        JpaUtil.bindQueryParams(query, posParams);
        return query.getResultList();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

@SuppressWarnings("unchecked")
public UserSummaryListImpl getUserSummaryByTypeAndMediaPackage(String type, String mediapackageId) {
    UserSummaryListImpl result = new UserSummaryListImpl();
    EntityManager em = null;
    try {//from   w w w .  j  a  v  a2s  .co  m
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("userSummaryByMediapackageByType");
        q.setParameter("type", type);
        q.setParameter("mediapackageId", mediapackageId);
        List<Object[]> users = q.getResultList();
        for (Object[] user : users) {
            UserSummaryImpl userSummary = new UserSummaryImpl();
            userSummary.ingest(user);
            result.add(userSummary);
        }
        return result;
    } catch (Exception e) {
        logger.warn("Unable to return any results from mediapackage " + mediapackageId + " of type " + type
                + " because of ", e);
        return result;
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}

From source file:org.opencastproject.usertracking.impl.UserTrackingServiceImpl.java

@SuppressWarnings("unchecked")
public UserActionList getUserActions(int offset, int limit) {
    UserActionList result = new UserActionListImpl();

    result.setTotal(getTotal());/* w  w  w.jav a 2s  . c  o m*/
    result.setOffset(offset);
    result.setLimit(limit);

    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("findUserActions");
        q.setFirstResult(offset);
        q.setMaxResults(limit);
        Collection<UserAction> userActions = q.getResultList();

        for (UserAction a : userActions) {
            result.add(a);
        }

        return result;
    } finally {
        if (em != null && em.isOpen()) {
            em.close();
        }
    }
}