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.headissue.pigeon.util.JPAUtils.java

/**
 * Returns a single entity. Note: if the entity does not exist then return null
 *//*w  ww . ja  v  a 2  s  .c  o  m*/
public static <T> T getSingleResult(TypedQuery<T> q) {
    try {
        return q.getSingleResult();
    } catch (Exception e) {
        LogUtils.trace(log, e, "access to a single entity is failed");
        return null;
    }
}

From source file:com.enioka.jqm.tools.ResourceParser.java

private static JndiResourceDescriptor fromDatabase(String alias) throws NamingException {
    JndiObjectResource resource = null;//from w  w w. j av a  2  s  .c  om
    EntityManager em = null;
    try {
        // Using the horrible CriteriaBuilder API instead of a string query. This avoids classloading issues - Hibernate binds
        // the entities at run time with the thread current classloader...
        em = Helpers.getNewEm();

        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriaQuery<JndiObjectResource> q = cb.createQuery(JndiObjectResource.class);
        Root<JndiObjectResource> c = q.from(JndiObjectResource.class);
        ParameterExpression<String> p = cb.parameter(String.class);
        q.select(c).where(cb.equal(c.get("name"), p));

        TypedQuery<JndiObjectResource> query = em.createQuery(q);
        query.setParameter(p, alias);
        resource = query.getSingleResult();
    } catch (Exception e) {
        NamingException ex = new NamingException("Could not find a JNDI object resource of name " + alias);
        ex.setRootCause(e);
        throw ex;
    } finally {
        if (em != null) {
            em.close();
        }
    }

    // Create the ResourceDescriptor from the JPA object
    JndiResourceDescriptor d = new JndiResourceDescriptor(resource.getType(), resource.getDescription(), null,
            resource.getAuth(), resource.getFactory(), resource.getSingleton());
    for (JndiObjectResourceParameter prm : resource.getParameters()) {
        d.add(new StringRefAddr(prm.getKey(), prm.getValue()));
    }

    return d;
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static int getHistoryAllCount(EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM History h", Long.class);
    return q.getSingleResult().intValue();
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static int getQueueAllCount(EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM JobInstance h", Long.class);
    return q.getSingleResult().intValue();
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static int getOkCount(EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM History h WHERE h.status = 'ENDED'", Long.class);
    return q.getSingleResult().intValue();
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static boolean testOkCount(long theoreticalOkCount, EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM History h WHERE h.status = 'ENDED'", Long.class);
    return q.getSingleResult() == theoreticalOkCount;
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static int getNonOkCount(EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM History h WHERE h.status != 'ENDED'", Long.class);
    return q.getSingleResult().intValue();
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static void waitFor(long nbHistories, int timeoutMs, EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(h) FROM History h", Long.class);

    Calendar start = Calendar.getInstance();
    while (q.getSingleResult() < nbHistories
            && Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis() <= timeoutMs) {
        try {/*from w  w  w . java  2s .c o  m*/
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
}

From source file:com.enioka.jqm.test.helpers.TestHelpers.java

public static void waitForRunning(long nbJobInstances, int timeoutMs, EntityManager em) {
    TypedQuery<Long> q = em.createQuery("SELECT COUNT(ji) FROM JobInstance ji WHERE ji.state = 'RUNNING'",
            Long.class);

    Calendar start = Calendar.getInstance();
    while (q.getSingleResult() < nbJobInstances
            && Calendar.getInstance().getTimeInMillis() - start.getTimeInMillis() <= timeoutMs) {
        try {//from  w ww .  ja  v a 2s .  c  o m
            Thread.sleep(100);
        } catch (InterruptedException e) {
        }
    }
}

From source file:org.kew.rmf.matchconf.Configuration.java

public static long countDedupConfigs() {
    TypedQuery<Long> q = entityManager().createQuery(
            "SELECT COUNT(o) FROM Configuration AS o WHERE o.authorityFileName = :authorityFileName",
            Long.class);
    q.setParameter("authorityFileName", "");
    return q.getSingleResult();
}