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.jdbc.MonitoringJdbcStateStore.java

public void deleteMonitoringFeed(String feedName) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);//from   www.  ja  v  a2 s  .com
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_MONITORED_INSTANCES);
    q.setParameter("feedName", feedName);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public void deletePendingInstances(String feedName, String clusterName) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);/*from   www  . j av a 2s . c o  m*/
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_ALL_INSTANCES_FOR_FEED);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:com.romb.hashfon.helper.Helper.java

public boolean isLogged(String[] token) {
    try {//ww  w .  j a  v  a2  s .co  m
        EntityManager em = getEntityManager();
        if (token[1].equals("STUDENT")) {
            Student s = em.find(Student.class, Long.parseLong(token[2]));
            return s != null;
        } else {
            Hr hr = (Hr) em.createNamedQuery("Hr.findById").setParameter("id", Long.parseLong(token[2]))
                    .getSingleResult();
            return hr != null;
        }
    } catch (Exception e) {
        return false;
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public void deletePendingInstance(String feedName, String clusterName, Date nominalTime) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);// www.java  2 s. c  o m
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_PENDING_NOMINAL_INSTANCES);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    q.setParameter("nominalTime", nominalTime);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public void updateSLAAlertInstance(String feedName, String clusterName, Date nominalTime) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);//from ww w  .j av  a  2 s  .  c  o m
    Query q = entityManager.createNamedQuery(PersistenceConstants.UPDATE_SLA_HIGH);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    q.setParameter("nominalTime", nominalTime);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public void deleteFeedAlertInstance(String feedName, String clusterName, Date nominalTime) {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);//from w w w .j  a v  a  2  s  .c  o  m
    Query q = entityManager.createNamedQuery(PersistenceConstants.DELETE_FEED_ALERT_INSTANCE);
    q.setParameter("feedName", feedName);
    q.setParameter("clusterName", clusterName);
    q.setParameter("nominalTime", nominalTime);
    try {
        q.executeUpdate();
    } finally {
        commitAndCloseTransaction(entityManager);
    }
}

From source file:org.opencastproject.migration.SeriesMigrationService.java

@SuppressWarnings("unchecked")
public void migrateSeriesAcl(ComponentContext cc) throws SeriesServiceDatabaseException, IOException {
    logger.info("Start series ACL database migration from 1.3 to 1.4");
    String systemAccount = cc.getBundleContext().getProperty("org.opencastproject.security.digest.user");

    EntityManager em = emf.createEntityManager();
    Query query = em.createNamedQuery("Series.findAll");
    List<SeriesEntity> seriesEntities = null;
    try {//from   ww w.ja va 2s  .c om
        seriesEntities = (List<SeriesEntity>) query.getResultList();
    } catch (Exception e) {
        logger.error("Could not retrieve all series: {}", e.getMessage());
        throw new SeriesServiceDatabaseException(e);
    } finally {
        if (em != null)
            em.close();
    }

    if (seriesEntities.size() > 0) {
        logger.info("Found {} series ACL to migrate", seriesEntities.size());
        int totalMigrated = 0;
        int failed = 0;
        for (SeriesEntity entity : seriesEntities) {

            // Check if the series ACL has already been migrated
            if (entity.getAccessControl() != null) {
                try {
                    if (AccessControlParser.parseAcl(entity.getAccessControl()).getEntries().size() > 0) {
                        totalMigrated++;
                        logger.info("Skiped migrating already migrated series '{}' ({}/{})",
                                new Object[] { entity.getSeriesId(), totalMigrated, seriesEntities.size() });
                        continue;
                    }
                } catch (AccessControlParsingException e) {
                    // Parsing a 1.3 ACL with the 1.4 code could be throw an exception, so ignore it
                }
            }
            try {
                Organization organization = orgDirectoryService.getOrganization(entity.getOrganization());
                securityService.setOrganization(organization);
                securityService.setUser(SecurityUtil.createSystemUser(systemAccount, organization));
                seriesService.storeSeriesAccessControl(entity.getSeriesId(),
                        parse13Acl(entity.getAccessControl()));
                totalMigrated++;
                logger.info("Successfully migrated ACL of series {}: {}/{}",
                        new Object[] { entity.getSeriesId(), totalMigrated, seriesEntities.size() });
            } catch (NotFoundException e) {
                failed++;
                logger.error("Could not retrieve series {}: {}", entity.getSeriesId(), e.getMessage());
                throw new SeriesServiceDatabaseException(e);
            } catch (AccessControlParsingException e) {
                failed++;
                logger.error("Could not parse the ACL from series {}: {}", entity.getSeriesId(),
                        e.getMessage());
            } finally {
                securityService.setOrganization(null);
                securityService.setUser(null);
            }
        }
        logger.info(
                "Finished database migration of series ACL from 1.3 to 1.4. {} entries migrated. {} items couldn't be migrated. Check logs for errors",
                new Object[] { totalMigrated, failed });
    } else {
        logger.info("Finished database migration of series ACL from of 1.3 to 1.4. Nothing found to migrate");
    }
}

From source file:org.apache.falcon.jdbc.MonitoringJdbcStateStore.java

public List<FeedSLAAlertBean> getSLAHighCandidates() {
    EntityManager entityManager = getEntityManager();
    beginTransaction(entityManager);/*  ww  w  .j a  v  a  2  s.c  o m*/
    Query q = entityManager.createNamedQuery(PersistenceConstants.GET_SLA_HIGH_CANDIDATES);
    List result = q.getResultList();

    try {
        if (CollectionUtils.isEmpty(result)) {
            return null;
        }
    } finally {
        entityManager.close();
    }
    return result;
}

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

public List executeNativeNamedQuery(final String nnq, final Object[] params) {
    Object ret = this.getJpaTemplate().execute(new JpaCallback() {

        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createNamedQuery(nnq);
            int parameterIndex = 1;
            if (params != null && params.length > 0) {
                for (Object obj : params) {
                    query.setParameter(parameterIndex++, obj);
                }// w w w .  j a  va 2s  . c o  m
            }
            return query.getResultList();
        }
    });
    return (List) ret;
}

From source file:de.zib.gndms.GORFX.context.service.globus.resource.ExtTaskResourceHome.java

private void resumeTasks() throws Exception {

    logger.debug("Checking for aborted tasks.");
    EntityManager em = null;
    List<String> rs = null;
    try {/*from  ww  w . j  a v  a 2  s. c o m*/
        em = system.getEntityManagerFactory().createEntityManager();
        Query q = em.createNamedQuery("unfinishedTaskIds");
        rs = q.getResultList();
        if (rs.size() == 0) {
            logger.debug("No tasks found :-)");
            return;
        }
    } finally {
        if (em != null && em.isOpen())
            em.close();
    }

    logger.debug("Try to resume " + rs.size() + " tasks");
    for (String id : rs) {
        logger.debug("Resuming " + id);
        Resource k = find(getKeyForId(id));
    }
}