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.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public Collection<InstanceState> getAllExecutionInstances(Entity entity, String cluster)
        throws StateStoreException {
    EntityClusterID id = new EntityClusterID(entity, cluster);
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_INSTANCES_FOR_ENTITY_CLUSTER);
    q.setParameter("entityId", id.getEntityID().getKey());
    q.setParameter("cluster", cluster);
    List result = q.getResultList();
    entityManager.close();// w w  w  .  j ava  2s.  c o  m
    try {
        return BeanMapperUtil.convertToInstanceState(result);
    } catch (IOException e) {
        throw new StateStoreException(e);
    }
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public InstanceState getLastExecutionInstance(Entity entity, String cluster) throws StateStoreException {
    String key = new EntityClusterID(entity, cluster).getEntityID().getKey();
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_LAST_INSTANCE_FOR_ENTITY_CLUSTER);
    q.setParameter("entityId", key);
    q.setParameter("cluster", cluster);
    q.setMaxResults(1);//www  .  jav a 2 s .co m
    List result = q.getResultList();
    entityManager.close();
    if (!result.isEmpty()) {
        try {
            return BeanMapperUtil.convertToInstanceState((InstanceBean) result.get(0));
        } catch (IOException e) {
            throw new StateStoreException(e);
        }
    }
    return null;
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public Collection<InstanceState> getExecutionInstances(EntityClusterID id,
        Collection<InstanceState.STATE> states) throws StateStoreException {
    String entityKey = id.getEntityID().getKey();
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_INSTANCES_FOR_ENTITY_FOR_STATES);
    q.setParameter("entityId", entityKey);
    List<String> instanceStates = new ArrayList<>();
    for (InstanceState.STATE state : states) {
        instanceStates.add(state.toString());
    }/*from w  ww  .  j  av a2  s  . c  o m*/
    q.setParameter("currentState", instanceStates);
    List result = q.getResultList();
    entityManager.close();
    try {
        return BeanMapperUtil.convertToInstanceState(result);
    } catch (IOException e) {
        throw new StateStoreException(e);
    }
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public InstanceState getExecutionInstance(String externalID) throws StateStoreException {
    if (StringUtils.isEmpty(externalID)) {
        throw new StateStoreException("External ID for retrieving instance cannot be null or empty");
    }/*from  ww  w  .  j ava2s.  c o m*/
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_INSTANCE_FOR_EXTERNAL_ID);
    q.setParameter("externalID", externalID);
    List result = q.getResultList();
    entityManager.close();
    if (result.isEmpty()) {
        return null;
    }
    try {
        InstanceBean instanceBean = (InstanceBean) (result.get(0));
        return BeanMapperUtil.convertToInstanceState(instanceBean);
    } catch (IOException e) {
        throw new StateStoreException(e);
    }

}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public Collection<InstanceState> getExecutionInstances(Entity entity, String cluster,
        Collection<InstanceState.STATE> states) throws StateStoreException {
    EntityClusterID entityClusterID = new EntityClusterID(entity, cluster);
    String entityKey = entityClusterID.getEntityID().getKey();
    EntityManager entityManager = getEntityManager();
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_INSTANCES_FOR_ENTITY_CLUSTER_FOR_STATES);
    q.setParameter("entityId", entityKey);
    q.setParameter("cluster", cluster);
    List<String> instanceStates = new ArrayList<>();
    for (InstanceState.STATE state : states) {
        instanceStates.add(state.toString());
    }//from www .ja v  a2s .c o m
    q.setParameter("currentState", instanceStates);
    List result = q.getResultList();
    entityManager.close();
    try {
        return BeanMapperUtil.convertToInstanceState(result);
    } catch (IOException e) {
        throw new StateStoreException(e);
    }
}

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

/**
 * Gets a search entity by it's id, using the current organizational context.
 *
 * @param id//from   w  ww . java 2s  . c  om
 *          the media package identifier
 * @param em
 *          an open entity manager
 * @return the search entity, or null if not found
 */
private SearchEntity getSearchEntity(String id, EntityManager em) {
    Query q = em.createNamedQuery("Search.findById").setParameter("mediaPackageId", id);
    try {
        return (SearchEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public Collection<InstanceState> getExecutionInstances(Entity entity, String cluster,
        Collection<InstanceState.STATE> states, DateTime start, DateTime end) throws StateStoreException {
    String entityKey = new EntityClusterID(entity, cluster).getEntityID().getKey();
    EntityManager entityManager = getEntityManager();
    Query q = entityManager
            .createNamedQuery(PersistenceConstants.GET_INSTANCES_FOR_ENTITY_CLUSTER_FOR_STATES_WITH_RANGE);
    q.setParameter("entityId", entityKey);
    List<String> instanceStates = new ArrayList<>();
    for (InstanceState.STATE state : states) {
        instanceStates.add(state.toString());
    }/*from   w w  w. ja va2 s.  c  o  m*/
    q.setParameter("currentState", instanceStates);
    q.setParameter("startTime", new Timestamp(start.getMillis()));
    q.setParameter("endTime", new Timestamp(end.getMillis()));
    q.setParameter("cluster", cluster);
    List result = q.getResultList();
    entityManager.close();
    try {
        return BeanMapperUtil.convertToInstanceState(result);
    } catch (IOException e) {
        throw new StateStoreException(e);
    }
}

From source file:org.opencastproject.series.impl.persistence.SeriesServiceDatabaseImpl.java

/**
 * Gets a series by its ID, using the current organizational context.
 * //ww  w  . ja v  a 2s . c om
 * @param id
 *          the series identifier
 * @param em
 *          an open entity manager
 * @return the series entity, or null if not found
 */
protected SeriesEntity getSeriesEntity(String id, EntityManager em) {
    String orgId = securityService.getOrganization().getId();
    Query q = em.createNamedQuery("seriesById").setParameter("seriesId", id).setParameter("organization",
            orgId);
    try {
        return (SeriesEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public void deleteExecutionInstances(EntityID entityID) {
    String entityKey = entityID.getKey();
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);//from www .ja  v a 2s  .  com
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_INSTANCE_FOR_ENTITY);
    q.setParameter("entityId", entityKey);
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}

From source file:org.apache.falcon.state.store.jdbc.JDBCStateStore.java

@Override
public void deleteEntities() throws StateStoreException {
    if (!isModeDebug()) {
        throw new UnsupportedOperationException("Delete Entities Table not supported");
    }/*from  www. ja  va  2s  .  c  o m*/
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_ENTITIES);
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}