Example usage for javax.persistence Query getSingleResult

List of usage examples for javax.persistence Query getSingleResult

Introduction

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

Prototype

Object getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single untyped result.

Usage

From source file:com.healthcit.cacure.dao.FormElementDao.java

/**
 * @return Next Ord Number in ordered entities.
 *//*from  w ww . j  a va2s.com*/
@Transactional(propagation = Propagation.SUPPORTS)
public Integer calculateNextOrdNumber(Long formId) {
    String sql = "select max(ord +1) from form_element where form_id = :formId";
    Query query = em.createNativeQuery(sql);
    query.setParameter("formId", formId);
    BigInteger o = (BigInteger) query.getSingleResult();
    if (o == null) {
        o = BigInteger.valueOf(1l);
    }

    return o == null ? null : Integer.valueOf(o.intValue());
}

From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java

@SuppressWarnings("unchecked")
public Device findDeviceById(Long id) {
    Query query = getEntityManager().createNamedQuery("Device.findDevicesById");
    query.setParameter("id", id);
    Device result;//w ww .ja  v  a  2  s.c o m
    try {
        result = (Device) query.getSingleResult();
    } catch (Exception e) {
        LOG.info("Exception while trying to find device", e);
        result = null;
    }
    return result;
}

From source file:com.healthcit.cacure.dao.FormElementDao.java

public FormElement findFormElementById(Long id) {
    Query query = em.createQuery("from FormElement fe where id = :Id");
    query.setParameter("Id", id);
    return (FormElement) query.getSingleResult();
}

From source file:edu.umm.radonc.ca_dash.model.TxInstanceFacade.java

public int itemsDateRangeCount(Date startDate, Date endDate) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery(TxInstance.class);
    Root<TxInstance> rt = cq.from(TxInstance.class);
    cq.select(cb.count(rt.get(TxInstance_.activityinstanceser)));
    cq.where(cb.and(rt.get(TxInstance_.completed).isNotNull(),
            cb.between(rt.get(TxInstance_.completed), startDate, endDate)));

    Query q = em.createQuery(cq);
    return ((Long) (q.getSingleResult())).intValue();
}

From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java

@SuppressWarnings("unchecked")
public Device findDeviceByRegId(String regid) {
    Query query = getEntityManager().createNamedQuery("Device.findDeviceByRegId");
    query.setParameter("regId", regid);
    Device result;/*from   www  . ja v  a  2s .  c om*/
    try {
        result = (Device) query.getSingleResult();
    } catch (Exception e) {
        LOG.info("query.getSingleResult() in FindDeviceByRegId failed to return a specific Device.");
        result = null;
    }
    return result;
}

From source file:com.openmeap.model.ModelServiceImpl.java

@Override
public int countApplicationArchivesByHashAndHashAlg(String hash, String hashAlg) {
    Query q = entityManager.createQuery("select count(ar) " + "from ApplicationArchive ar "
            + "where ar.hash=:hash " + "and ar.hashAlgorithm=:hashAlgorithm ");
    q.setParameter("hash", hash);
    q.setParameter("hashAlgorithm", hashAlg);
    try {/*from   ww w. jav  a  2  s .  com*/
        Number ret = (Number) q.getSingleResult();
        return ret.intValue();
    } catch (NoResultException nre) {
        return 0;
    }
}

From source file:eu.forgestore.ws.impl.FStoreJpaController.java

public long countUsers() {

    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Query q = entityManager.createQuery("SELECT COUNT(s) FROM FStoreUser s");
    return (Long) q.getSingleResult();
}

From source file:org.opencastproject.userdirectory.jpa.JpaUserAndRoleProvider.java

@PUT
@Path("{username}.json")
@RestQuery(name = "roleupdate", description = "Updates a user's roles", returnDescription = "No content", restParameters = @RestParameter(name = "roles", type = TEXT, isRequired = true, description = "The user roles as a json array"), pathParameters = @RestParameter(name = "username", type = STRING, isRequired = true, description = "The username"), reponses = {
        @RestResponse(responseCode = SC_NO_CONTENT, description = "The user roles have been updated.") })
public Response updateUserFromJson(@PathParam("username") String username, @FormParam("roles") String roles) {
    JSONArray rolesArray = (JSONArray) JSONValue.parse(roles);
    EntityManager em = null;//from w  w w  .  j a v a 2  s. com
    EntityTransaction tx = null;
    try {
        em = emf.createEntityManager();
        tx = em.getTransaction();
        tx.begin();
        // Find the existing user
        Query q = em.createNamedQuery("user");
        q.setParameter("u", username);
        q.setParameter("o", securityService.getOrganization().getId());
        JpaUser jpaUser = null;
        try {
            jpaUser = (JpaUser) q.getSingleResult();
            jpaUser.roles.clear();
            for (Object role : rolesArray) {
                jpaUser.roles.add((String) role);
            }
            em.merge(jpaUser);
        } catch (NoResultException e) {
            return null; // this will be translated into a 404
        }
        tx.commit();
        return Response.noContent().build();
    } finally {
        if (tx.isActive()) {
            tx.rollback();
        }
        if (em != null)
            em.close();
    }
}

From source file:com.healthcit.cacure.dao.FormElementDao.java

public FormElement getById(Long id) {
    Query query = em.createQuery("select fe from FormElement fe where id = :Id");
    query.setParameter("Id", id);
    return (FormElement) query.getSingleResult();
}

From source file:eu.trentorise.smartcampus.ac.provider.repository.persistence.AcDaoPersistenceImpl.java

/**
 * Returns authority by its name or null if no authority exists by the given
 * name/* ww w .j a  v a  2  s  . c  om*/
 * 
 * @param name
 *            name of authority to search
 * @return the authority or null if it doens't exist
 */
@Override
public Authority readAuthorityByName(String name) {
    try {
        Query query = em.createQuery("from AuthorityEntity a where a.name = :name");
        query.setParameter("name", name);
        try {
            return PersistenceConverter.toAuthority((AuthorityEntity) query.getSingleResult());
        } catch (NoResultException e) {
            return null;
        }
    } catch (NullPointerException e) {
        return null;
    }
}