Example usage for javax.persistence TypedQuery getSingleResult

List of usage examples for javax.persistence TypedQuery getSingleResult

Introduction

In this page you can find the example usage for javax.persistence TypedQuery getSingleResult.

Prototype

X getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single result.

Usage

From source file:com.netflix.genie.server.metrics.impl.JobCountManagerImpl.java

/**
 * {@inheritDoc}//from  www  .  j ava 2s  . com
 */
@Override
@Transactional(readOnly = true)
//TODO: Move to specification
public int getNumInstanceJobs(final String hostName, final Long minStartTime, final Long maxStartTime)
        throws GenieException {
    LOG.debug("called");

    final String finalHostName;
    // initialize host name
    if (StringUtils.isBlank(hostName)) {
        finalHostName = NetUtil.getHostName();
    } else {
        finalHostName = hostName;
    }
    final CriteriaBuilder cb = this.em.getCriteriaBuilder();
    final CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    final Root<Job> j = cq.from(Job.class);
    cq.select(cb.count(j));
    final Predicate runningStatus = cb.equal(j.get(Job_.status), JobStatus.RUNNING);
    final Predicate initStatus = cb.equal(j.get(Job_.status), JobStatus.INIT);
    final List<Predicate> predicates = new ArrayList<>();
    predicates.add(cb.equal(j.get(Job_.hostName), finalHostName));
    predicates.add(cb.or(runningStatus, initStatus));
    if (minStartTime != null) {
        predicates.add(cb.greaterThanOrEqualTo(j.get(Job_.started), new Date(minStartTime)));
    }
    if (maxStartTime != null) {
        predicates.add(cb.lessThan(j.get(Job_.started), new Date(maxStartTime)));
    }
    //documentation says that by default predicate array is conjuncted together
    cq.where(predicates.toArray(new Predicate[predicates.size()]));
    final TypedQuery<Long> query = this.em.createQuery(cq);
    //Downgrading to an int since all the code seems to want ints
    //Don't feel like changing everthing right now can figure out later
    //if need be
    return query.getSingleResult().intValue();
}

From source file:org.openmeetings.app.data.user.dao.PrivateMessageFolderDaoImpl.java

public PrivateMessageFolder getPrivateMessageFolderById(Long privateMessageFolderId) {
    try {/*from w w w. j a v a2 s. co m*/
        String hql = "select c from PrivateMessageFolder c "
                + "where c.privateMessageFolderId = :privateMessageFolderId ";

        TypedQuery<PrivateMessageFolder> query = em.createQuery(hql, PrivateMessageFolder.class);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);

        PrivateMessageFolder privateMessageFolder = null;
        try {
            privateMessageFolder = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return privateMessageFolder;
    } catch (Exception e) {
        log.error("[getPrivateMessageFolderById]", e);
    }
    return null;
}

From source file:net.echinopsii.ariane.community.plugin.rabbitmq.directory.RabbitmqDirectoryBootstrap.java

private void plugDirectoryJPAProvider() {
    Company pivotal = null;/*  w ww .  j ava 2s. c  o  m*/
    Application rabbitmq = null;

    directoryJpaProvider.addSubPersistenceBundle(FrameworkUtil.getBundle(RabbitmqDirectoryBootstrap.class));

    EntityManager em = directoryJpaProvider.createEM();
    CriteriaBuilder builder = em.getCriteriaBuilder();

    CriteriaQuery<Company> cmpCriteria = builder.createQuery(Company.class);
    Root<Company> cmpRoot = cmpCriteria.from(Company.class);
    cmpCriteria.select(cmpRoot).where(builder.equal(cmpRoot.<String>get("name"), "Pivotal"));
    TypedQuery<Company> cmpQuery = em.createQuery(cmpCriteria);
    try {
        pivotal = cmpQuery.getSingleResult();
        log.debug("Pivotal company already defined ...");
    } catch (NoResultException e) {
        log.debug("Pivotal company will be defined ...");
    } catch (Exception e) {
        throw e;
    }

    CriteriaQuery<Application> appCriteria = builder.createQuery(Application.class);
    Root<Application> appRoot = appCriteria.from(Application.class);
    appCriteria.select(appRoot).where(builder.equal(appRoot.<String>get("name"), "RabbitMQ"));
    TypedQuery<Application> appQuery = em.createQuery(appCriteria);
    try {
        rabbitmq = appQuery.getSingleResult();
        log.debug("RabbitMQ application already defined ...");
    } catch (NoResultException e) {
        log.debug("RabbitMQ application will be defined ...");
    } catch (Exception e) {
        throw e;
    }

    em.getTransaction().begin();

    if (pivotal == null) {
        pivotal = new Company().setNameR("Pivotal").setDescriptionR("Pivotal");
        em.persist(pivotal);
    }

    if (rabbitmq == null) {
        rabbitmq = new Application().setNameR("RabbitMQ").setCompanyR(pivotal).setShortNameR("RabbitMQ")
                .setColorCodeR("ff6600").setDescriptionR("Robust messaging for applications");
        em.persist(rabbitmq);
    }

    if (!pivotal.getApplications().contains(rabbitmq)) {
        pivotal.getApplications().add(rabbitmq);
    }

    em.flush();
    em.getTransaction().commit();
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

@Override
public int getRetentionDownloadedByMpcName(final String mpcName) {
    final TypedQuery<Mpc> query = entityManager.createNamedQuery("Mpc.findByQualifiedName", Mpc.class);
    query.setParameter("QUALIFIED_NAME", mpcName);

    final Mpc result = query.getSingleResult();

    if (result == null) {
        PModeDao.LOG.error("No mpc with name: " + mpcName
                + " found. Assuming message retention of 0 for downloaded messages.");
        return 0;
    }//  w  ww  . j  a  va 2s  . co  m

    return result.getRetentionDownloaded();
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

@Override
public int getRetentionUndownloadedByMpcName(final String mpcName) {
    final TypedQuery<Mpc> query = this.entityManager.createNamedQuery("Mpc.findByQualifiedName", Mpc.class);
    query.setParameter("QUALIFIED_NAME", mpcName);
    final Mpc result = query.getSingleResult();

    if (result == null) {
        PModeDao.LOG.error("No mpc with name: " + mpcName
                + " found. Assuming message retention of -1 for undownloaded messages.");
        return 0;
    }/*from   ww  w  . j  a  v  a  2 s.c o m*/

    return result.getRetentionUndownloaded();
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

@Override
public Action getAction(final String pModeKey) {
    final TypedQuery<Action> query = this.entityManager.createNamedQuery("Action.findByName", Action.class);
    query.setParameter("NAME", this.getActionNameFromPModeKey(pModeKey));
    return query.getSingleResult();
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

@Override
public Party getSenderParty(final String pModeKey) {
    final TypedQuery<Party> query = this.entityManager.createNamedQuery("Party.findByName", Party.class);
    query.setParameter("NAME", this.getSenderPartyNameFromPModeKey(pModeKey));
    return query.getSingleResult();
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

@Override
public Party getReceiverParty(final String pModeKey) {
    final TypedQuery<Party> query = this.entityManager.createNamedQuery("Party.findByName", Party.class);
    query.setParameter("NAME", this.getReceiverPartyNameFromPModeKey(pModeKey));
    return query.getSingleResult();
}

From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java

@Transactional(propagation = Propagation.SUPPORTS)
protected <E> E findGenericSingleResult(TypedQuery<E> query) throws NotFoundException {
    try {//from w  w  w.j a  v a 2s  .com
        return query.getSingleResult();
    } catch (NoResultException e) {
        throw new NotFoundException("Result of " + query + " with parameters '"
                + Arrays.toString(query.getParameters().toArray()) + "' not found in database", e);
    }
}

From source file:eu.ggnet.dwoss.report.ReportAgentBean.java

@Override
public long count(SearchParameter search) {
    StringBuilder sb = new StringBuilder("Select Count(l) from ReportLine l");
    if (!StringUtils.isBlank(search.getRefurbishId()))
        sb.append(" where l.refurbishId = :refurbishId");
    L.debug("Using created SearchQuery:{}", sb);
    TypedQuery<Long> q = reportEm.createQuery(sb.toString(), Long.class);
    if (!StringUtils.isBlank(search.getRefurbishId()))
        q.setParameter("refurbishId", search.getRefurbishId().trim());
    return q.getSingleResult();
}