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.apache.roller.weblogger.business.jpa.JPAPersistenceStrategy.java

/**
 * Get named update query with default flush mode
 * @param queryName the name of the query
 * @throws org.apache.roller.weblogger.WebloggerException on any error
 *//*from  w ww  . ja  va2  s  . c  o m*/
public Query getNamedUpdate(String queryName) throws WebloggerException {
    EntityManager em = getEntityManager(true);
    Query q = em.createNamedQuery(queryName);
    return q;
}

From source file:facades.PersonFacadeDB.java

@Override
public String getPersons() {

    String result = "";
    EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceFileName);
    EntityManager em = emf.createEntityManager();
    EntityTransaction transaction = em.getTransaction();
    transaction.begin();/*from ww  w.  j  a va  2  s .c  o m*/

    try {
        Query query = em.createNamedQuery("Person.findAll");
        List<Person> people = query.getResultList();

        try {
            result = om.writeValueAsString(people);
        } catch (JsonProcessingException ex) {
            Logger.getLogger(PersonFacadeDB.class.getName()).log(Level.SEVERE, null, ex);
        }

    } finally {
        em.close();
    }
    return result;
}

From source file:org.apache.roller.planet.business.jpa.JPAPersistenceStrategy.java

/**
 * Get named update query with default flush mode
 * @param queryName the name of the query
 * @throws org.apache.roller.planet.PlanetException on any error
 *//*www .j ava 2s .  c  om*/
public Query getNamedUpdate(String queryName) throws PlanetException {
    EntityManager em = getEntityManager(true);
    Query q = em.createNamedQuery(queryName);
    return q;
}

From source file:org.apache.roller.weblogger.business.jpa.JPAPersistenceStrategy.java

/**
 * Get named query with FlushModeType.COMMIT
 * @param queryName the name of the query
 * @throws org.apache.roller.weblogger.WebloggerException on any error
 *///from   ww  w  .  ja v  a  2 s  . c  om
public Query getNamedQuery(String queryName) throws WebloggerException {
    EntityManager em = getEntityManager(false);
    Query q = em.createNamedQuery(queryName);
    // Never flush for queries. Roller code assumes this behavior
    q.setFlushMode(FlushModeType.COMMIT);
    return q;
}

From source file:org.apache.roller.planet.business.jpa.JPAPersistenceStrategy.java

/**
 * Get named query with FlushModeType.COMMIT
 * @param queryName the name of the query
 * @throws org.apache.roller.planet.PlanetException on any error
 *///from   w  ww .  jav  a 2  s.  c o m
public Query getNamedQuery(String queryName) throws PlanetException {
    EntityManager em = getEntityManager(false);
    Query q = em.createNamedQuery(queryName);
    // Never flush for queries. Roller code assumes this behavior
    q.setFlushMode(FlushModeType.COMMIT);
    return q;
}

From source file:org.nuxeo.theme.webwidgets.providers.PersistentProviderPerUser.java

@Override
@SuppressWarnings("unchecked")
public synchronized List<Widget> getWidgets(final String regionName) throws ProviderException {
    if (regionName == null) {
        throw new ProviderException("Region name is undefined");
    }//  ww w .  java  2  s.com

    try {
        return getPersistenceProvider().run(false, new RunCallback<List<Widget>>() {
            public List<Widget> runWith(EntityManager em) {
                List<Widget> widgets = new ArrayList<Widget>();
                Principal currentNuxeoPrincipal = getCurrentPrincipal();
                if (currentNuxeoPrincipal != null) {
                    Query query = em.createNamedQuery("Widget.findByScope");
                    query.setParameter("region", regionName);
                    query.setParameter("scope", currentNuxeoPrincipal.getName());
                    widgets.addAll(query.getResultList());
                }
                return widgets;
            }
        });
    } catch (ClientException e) {
        throw new ProviderException(e);
    }

}

From source file:com.easyjf.core.dao.impl.GenericDAOImpl.java

public List executeNamedQuery(final String queryName, final Object[] params, final int begin, final int max) {
    List ret = (List) this.getJpaTemplate().execute(new JpaCallback() {

        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createNamedQuery(queryName);
            int parameterIndex = 1;
            if (params != null && params.length > 0) {
                for (Object obj : params) {
                    query.setParameter(parameterIndex++, obj);
                }/*from  ww w . java2  s. co  m*/
            }
            if (begin >= 0 && max > 0) {
                query.setFirstResult(begin);
                query.setMaxResults(max);
            }
            return query.getResultList();
        }
    });
    if (ret != null && ret.size() >= 0) {
        return ret;
    } else {
        return new ArrayList();
    }
}

From source file:org.opencastproject.kernel.security.persistence.OrganizationDatabaseImpl.java

/**
 * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#getOrganizations()
 *///from  ww  w .j  av a 2s . com
@Override
@SuppressWarnings("unchecked")
public List<Organization> getOrganizations() throws OrganizationDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("Organization.findAll");
        return q.getResultList();
    } catch (Exception e) {
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:org.opencastproject.kernel.security.persistence.OrganizationDatabaseImpl.java

/**
 * Return the persisted organization entity by its id
 * //from w  ww  .ja  v  a2 s  .co m
 * @param id
 *          the organization id
 * @return the organization or <code>null</code> if not found
 * @throws OrganizationDatabaseException
 *           if there is a problem communicating with the underlying data store
 */
private JpaOrganization getOrganizationEntity(String id) throws OrganizationDatabaseException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("Organization.findById");
        q.setParameter("id", id);
        return (JpaOrganization) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (Exception e) {
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:org.opencastproject.kernel.security.persistence.OrganizationDatabaseImpl.java

/**
 * @see org.opencastproject.kernel.security.persistence.OrganizationDatabase#getOrganizationByUrl(java.net.URL)
 *///from w  w  w . jav  a2 s. c  o  m
@Override
public Organization getOrganizationByUrl(URL url) throws OrganizationDatabaseException, NotFoundException {
    String requestUrl = StringUtils.strip(url.getHost(), "/");
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query q = em.createNamedQuery("Organization.findByUrl");
        q.setParameter("serverName", requestUrl);
        q.setParameter("port", url.getPort());
        return (JpaOrganization) q.getSingleResult();
    } catch (NoResultException e) {
        throw new NotFoundException();
    } catch (Exception e) {
        throw new OrganizationDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
}