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.serviceregistry.impl.ServiceRegistryJpaImpl.java

/**
 * Fetches a host registration from persistence.
 * // w  w  w.  jav a  2 s.c om
 * @param em
 *          an active entity manager
 * @param host
 *          the host name
 * @return the host registration, or null if none exists
 */
protected HostRegistrationJpaImpl fetchHostRegistration(EntityManager em, String host) {
    Query query = em.createNamedQuery("HostRegistration.byHostName");
    query.setParameter("host", host);
    try {
        return (HostRegistrationJpaImpl) query.getSingleResult();
    } catch (NoResultException e) {
        logger.debug("No existing host registration for {}", host);
        return null;
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

@SuppressWarnings("unchecked")
private List<Job> getChildren(EntityManager em, long id) throws Exception {
    Query query = em.createNamedQuery("Job.children");
    query.setParameter("id", id);
    List<Job> childJobs = query.getResultList();
    List<Job> resultJobs = new ArrayList<Job>(childJobs);
    for (Job childJob : childJobs) {
        resultJobs.addAll(getChildren(em, childJob.getId()));
    }/*from w  ww  .j a va 2 s.co  m*/
    return resultJobs;
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

protected ServiceRegistrationJpaImpl getServiceRegistration(EntityManager em, String serviceType, String host) {
    try {//from ww w .  j ava2s  .co m
        Query q = em.createNamedQuery("ServiceRegistration.getRegistration");
        q.setParameter("serviceType", serviceType);
        q.setParameter("host", host);
        return (ServiceRegistrationJpaImpl) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

@SuppressWarnings("unchecked")
private List<ServiceRegistration> getOnlineServiceRegistrations() {
    EntityManager em = null;
    try {/*from w w w.j  av  a 2 s.c  o m*/
        em = emf.createEntityManager();
        return em.createNamedQuery("ServiceRegistration.getAllOnline").getResultList();
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

/**
 * {@inheritDoc}//from   ww w .j  ava 2  s .com
 * 
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getServiceRegistrationsByHost(java.lang.String)
 */
@SuppressWarnings("unchecked")
@Override
public List<ServiceRegistration> getServiceRegistrationsByHost(String host) throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        return em.createNamedQuery("ServiceRegistration.getByHost").setParameter("host", host).getResultList();
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

/**
 * {@inheritDoc}//from  w w  w. j  av a  2  s . c om
 * 
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getServiceRegistrationsByType(java.lang.String)
 */
@SuppressWarnings("unchecked")
@Override
public List<ServiceRegistration> getServiceRegistrationsByType(String serviceType)
        throws ServiceRegistryException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        return em.createNamedQuery("ServiceRegistration.getByType").setParameter("serviceType", serviceType)
                .getResultList();
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

/**
 * {@inheritDoc}//from   ww  w.  j  ava  2 s  .  co  m
 * 
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#getMaxConcurrentJobs()
 */
@Override
public int getMaxConcurrentJobs() throws ServiceRegistryException {
    Query query = null;
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        query = em.createNamedQuery("HostRegistration.cores");
        return ((Number) query.getSingleResult()).intValue();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

@SuppressWarnings("unchecked")
protected List<Object[]> getAvgOperations(EntityManager em) throws ServiceRegistryException {
    Query query = null;/*from   w ww  .  j  a v  a 2s .  com*/
    try {
        query = em.createNamedQuery("Job.avgOperation");
        return query.getResultList();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

@SuppressWarnings("unchecked")
List<Object[]> getCountPerHostService(EntityManager em) throws ServiceRegistryException {
    Query query = null;//from   ww w.ja v  a 2 s. co m
    try {
        query = em.createNamedQuery("Job.countPerHostService");
        return query.getResultList();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    }
}

From source file:org.opencastproject.serviceregistry.impl.ServiceRegistryJpaImpl.java

/**
 * {@inheritDoc}//from w w w.  j a  v  a  2  s . c o m
 * 
 * @see org.opencastproject.serviceregistry.api.ServiceRegistry#count(java.lang.String, java.lang.String,
 *      java.lang.String, org.opencastproject.job.api.Job.Status)
 */
public long count(String serviceType, String host, String operation, Status status)
        throws ServiceRegistryException {
    if (StringUtils.isBlank(serviceType) || StringUtils.isBlank(host) || StringUtils.isBlank(operation)
            || status == null)
        throw new IllegalArgumentException("service type, host, operation, and status must be provided");
    Query query = null;
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        query = em.createNamedQuery("Job.fullMonty");
        query.setParameter("status", status);
        query.setParameter("serviceType", serviceType);
        query.setParameter("operation", operation);
        Number countResult = (Number) query.getSingleResult();
        return countResult.longValue();
    } catch (Exception e) {
        throw new ServiceRegistryException(e);
    } finally {
        if (em != null)
            em.close();
    }
}