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 void deleteExecutionInstances() {
    if (!isModeDebug()) {
        throw new UnsupportedOperationException("Delete Instances Table not supported");
    }//from   w  w w . j av a 2s  .c  o m
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_INSTANCES_TABLE);
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}

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

@Override
public void deleteEntity(EntityID entityID) throws StateStoreException {
    if (!entityExists(entityID)) {
        throw new StateStoreException("Entity with key, " + entityID.getKey() + " does not exist.");
    }/*from   w  w  w . j a  v  a  2  s  . co  m*/
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_ENTITY);
    q.setParameter("id", entityID.getKey());
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}

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

@Override
public void deleteExecutionInstance(InstanceID instanceID) throws StateStoreException {
    String instanceKey = instanceID.toString();
    if (!executionInstanceExists(instanceID)) {
        throw new StateStoreException("Instance with key, " + instanceKey + " does not exist.");
    }/*from  www. j a  va 2  s  . c o m*/
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_INSTANCE);
    q.setParameter("id", instanceKey);
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}

From source file:org.opencastproject.scheduler.impl.persistence.SchedulerServiceDatabaseImpl.java

@SuppressWarnings("unchecked")
@Override//from  w  w w .j a v a  2 s .c o m
public DublinCoreCatalog[] getAllEvents() throws SchedulerServiceDatabaseException {
    EntityManager em = null;
    Query query = null;
    List<EventEntity> eventEntities = null;
    try {
        em = emf.createEntityManager();
        query = em.createNamedQuery("Event.findAll");
        eventEntities = (List<EventEntity>) query.getResultList();
    } catch (Exception e) {
        logger.error("Could not retrieve all events: {}", e.getMessage());
        throw new SchedulerServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }
    List<DublinCoreCatalog> eventList = new LinkedList<DublinCoreCatalog>();
    try {
        for (EventEntity entity : eventEntities) {
            DublinCoreCatalog dc = parseDublinCore(entity.dublinCoreXML);
            eventList.add(dc);
        }
    } catch (Exception e) {
        logger.error("Could not parse event entity: {}", e.getMessage());
        throw new SchedulerServiceDatabaseException(e);
    }
    return eventList.toArray(new DublinCoreCatalog[eventList.size()]);
}

From source file:org.opencastproject.themes.persistence.ThemesServiceDatabaseImpl.java

@Override
public int countThemes() throws ThemesServiceDatabaseException {
    EntityManager em = null;
    try {//from  w  ww.j a v a 2  s  .  co m
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        Query q = em.createNamedQuery("Themes.count").setParameter("org", orgId);
        Number countResult = (Number) q.getSingleResult();
        return countResult.intValue();
    } catch (Exception e) {
        logger.error("Could not count themes: {}", ExceptionUtils.getStackTrace(e));
        throw new ThemesServiceDatabaseException(e);
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

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

@Override
public void updateEntity(EntityState entityState) throws StateStoreException {
    EntityID entityID = new EntityID(entityState.getEntity());
    if (!entityExists(entityID)) {
        throw new StateStoreException("Entity with key, " + entityID + " doesn't exists.");
    }//from  w  w w.  jav  a 2  s  .  c  om
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.UPDATE_ENTITY);
    q.setParameter("id", entityID.getKey());
    if (entityState.getCurrentState() != null) {
        q.setParameter("state", entityState.getCurrentState().toString());
    }
    q.setParameter("type", entityState.getEntity().getEntityType().toString());
    q.setParameter("name", entityState.getEntity().getName());
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}

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

@Override
public void updateExecutionInstance(InstanceState instanceState) throws StateStoreException {
    InstanceID id = new InstanceID(instanceState.getInstance());
    String key = id.toString();/*from   w  w w  .j  a  va  2s  . c om*/
    if (!executionInstanceExists(id)) {
        throw new StateStoreException("Instance with key, " + key + " does not exist.");
    }
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);
    Query q = entityManager.createNamedQuery(PersistenceConstants.UPDATE_INSTANCE);
    ExecutionInstance instance = instanceState.getInstance();
    q.setParameter("id", key);
    q.setParameter("cluster", instance.getCluster());
    q.setParameter("externalID", instance.getExternalID());
    q.setParameter("instanceTime", new Timestamp(instance.getInstanceTime().getMillis()));
    q.setParameter("creationTime", new Timestamp(instance.getCreationTime().getMillis()));
    if (instance.getActualEnd() != null) {
        q.setParameter("actualEndTime", new Timestamp(instance.getActualEnd().getMillis()));
    }
    q.setParameter("currentState", instanceState.getCurrentState().toString());
    if (instance.getActualStart() != null) {
        q.setParameter("actualStartTime", new Timestamp(instance.getActualStart().getMillis()));
    }
    q.setParameter("instanceSequence", instance.getInstanceSequence());
    if (instanceState.getInstance().getAwaitingPredicates() != null
            && !instanceState.getInstance().getAwaitingPredicates().isEmpty()) {
        try {
            q.setParameter("awaitedPredicates", BeanMapperUtil.getAwaitedPredicates(instanceState));
        } catch (IOException e) {
            throw new StateStoreException(e);
        }
    }
    if (instance.getProperties() != null && !instance.getProperties().isEmpty()) {
        try {
            q.setParameter("properties", BeanMapperUtil.getProperties(instanceState));
        } catch (IOException e) {
            throw new StateStoreException(e);
        }
    }
    q.executeUpdate();
    commitAndCloseTransaction(entityManager);
}

From source file:org.rhq.enterprise.server.core.CoreServerServiceImpl.java

/**
 * @see CoreServerService#getPluginArchive(String)
 *///from   www  . j ava  2 s  .c  o  m
@Override
public InputStream getPluginArchive(String pluginName) {
    EntityManager em = null;

    try {
        em = LookupUtil.getEntityManager();
        Query q = em.createNamedQuery(Plugin.QUERY_FIND_BY_NAME);
        q.setParameter("name", pluginName);
        Plugin plugin = (Plugin) q.getSingleResult();

        // we know our plugins are in a subdirectory within our downloads location
        return getFileContents(new File("rhq-plugins", plugin.getPath()).getPath());
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

From source file:org.opencastproject.adminui.usersettings.UserSettingsService.java

/**
 * @return Finds the total number of user settings for the current user.
 * @throws UserSettingsServiceException//from w  w w.j  ava  2  s  . c om
 */
private int getUserSettingsTotal() throws UserSettingsServiceException {
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        String orgId = securityService.getOrganization().getId();
        String username = securityService.getUser().getUsername();
        Query q = em.createNamedQuery("UserSettings.countByUserName").setParameter("username", username)
                .setParameter("org", orgId);
        Number countResult = (Number) q.getSingleResult();
        return countResult.intValue();
    } catch (Exception e) {
        logger.error("Could not count message signatures: %s", ExceptionUtils.getStackTrace(e));
        throw new UserSettingsServiceException(e);
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

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

/**
 * {@inheritDoc}/*from   w  ww  .j  a v  a 2  s .c  o m*/
 *
 * @see org.opencastproject.search.impl.persistence.SearchServiceDatabase#getAllMediaPackages()
 */
@Override
@SuppressWarnings("unchecked")
public Iterator<Tuple<MediaPackage, String>> getAllMediaPackages() throws SearchServiceDatabaseException {
    List<SearchEntity> searchEntities = null;
    EntityManager em = null;
    try {
        em = emf.createEntityManager();
        Query query = em.createNamedQuery("Search.findAll");
        searchEntities = (List<SearchEntity>) query.getResultList();
    } catch (Exception e) {
        logger.error("Could not retrieve all episodes: {}", e.getMessage());
        throw new SearchServiceDatabaseException(e);
    } finally {
        em.close();
    }
    List<Tuple<MediaPackage, String>> mediaPackageList = new LinkedList<Tuple<MediaPackage, String>>();
    try {
        for (SearchEntity entity : searchEntities) {
            MediaPackage mediaPackage = MediaPackageParser.getFromXml(entity.getMediaPackageXML());
            mediaPackageList.add(Tuple.tuple(mediaPackage, entity.getOrganization()));
        }
    } catch (Exception e) {
        logger.error("Could not parse series entity: {}", e.getMessage());
        throw new SearchServiceDatabaseException(e);
    }
    return mediaPackageList.iterator();
}