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:eu.trentorise.smartcampus.ac.provider.repository.persistence.AcDaoPersistenceImpl.java

/**
 * Returns user given its social id/*from w  w w  . jav  a 2  s  .c o m*/
 * 
 * @param socialId
 *            the social id of user
 * @return user or null if it doesn't exist
 */
@Override
public User readUserBySocialId(long socialId) {
    try {
        Query query = em.createQuery("from UserEntity u where u.socialId= :socialId");
        query.setParameter("socialId", socialId);
        try {
            return PersistenceConverter.toUser((UserEntity) query.getSingleResult());
        } catch (NoResultException e) {
            return null;
        }
    } catch (NullPointerException e) {
        return null;
    }
}

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

public long countInstalledBuns() {

    EntityManager entityManager = entityManagerFactory.createEntityManager();

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

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

/**
 * Returns authority given its url/*from  ww  w.j a  va2s .  com*/
 * 
 * @param url
 *            the url of authority to search
 * @return authority of null if it doesn't exist
 */
@Override
public Authority readAuthorityByUrl(String url) {
    try {
        Query query = em.createQuery("from AuthorityEntity a where a.redirectUrl = :url");
        query.setParameter("url", url);
        try {
            return PersistenceConverter.toAuthority((AuthorityEntity) query.getSingleResult());
        } catch (NoResultException e) {
            return null;
        }
    } catch (NullPointerException e) {
        return null;
    }
}

From source file:org.energyos.espi.common.repositories.jpa.ResourceRepositoryImpl.java

@Override
public <T extends IdentifiedObject> Long findIdByXPath(Long id1, Long id2, Long id3, Long id4, Class<T> clazz) {
    try {//ww w.  j  a v a  2 s  .  co  m
        String findIdByXPath = (String) clazz.getDeclaredField("QUERY_FIND_ID_BY_XPATH").get(String.class);
        Query query = em.createNamedQuery(findIdByXPath).setParameter("o1Id", id1).setParameter("o2Id", id2)
                .setParameter("o3Id", id3).setParameter("o4Id", id4);
        return (Long) query.getSingleResult();
    } catch (NoSuchFieldException | IllegalAccessException e) {
        System.out.printf("**** findIdByXPath(Long id1, Long id2, Long id3, Long id4) Exception: %s - %s\n",
                clazz.toString(), e.toString());
        throw new RuntimeException(e);
    }
}

From source file:org.synyx.hades.dao.test.UserDaoIntegrationTest.java

/**
 * Tests creation of users.//from   w  ww.j a  v  a2  s .  co  m
 */
@Test
public void testCreation() {

    Query countQuery = em.createQuery("select count(u) from User u");
    Long before = (Long) countQuery.getSingleResult();

    flushTestUsers();

    assertEquals(before + 3, countQuery.getSingleResult());
}

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

private boolean userExists(String authToken) {
    try {/*from  w w  w.  jav a2s. co m*/
        Query query = em.createQuery(
                "from UserEntity u where u.authToken= :authToken or u.sessionAuthToken = :authToken");
        query.setParameter("authToken", authToken);
        try {
            query.getSingleResult();
            return true;
        } catch (NoResultException e) {
            return false;
        }
    } catch (Exception e) {
        return false;
    }
}

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

@SuppressWarnings("unchecked")
public Device findDeviceByDeviceId(String deviceid) {
    // TODO this method is confusion when looking at the method above, both use the same query but
    // the above expects a list of results, and this method expects only one result.
    // If more than one results is returned this method will throw an exception
    Query query = getEntityManager().createNamedQuery("Device.findDevicesByDeviceId");
    query.setParameter("deviceId", deviceid);
    Device result = null;//from   w w  w.  j a  v a 2s. c o m
    try {
        result = (Device) query.getSingleResult();
    } catch (NoResultException e) {
        LOG.info("Device with specified id does not exist");
    }
    return result;
}

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

public FormElement getByUUID(String uuid) {
    Query query = em.createQuery("select fe from FormElement fe where uuid = :Id");
    query.setParameter("Id", uuid);
    try {/*w ww .j  a  v  a 2s. c o  m*/
        return (FormElement) query.getSingleResult();
    } catch (javax.persistence.NoResultException e) {
        return null;
    }
}

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

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

From source file:com.ushahidi.swiftriver.core.api.dao.impl.JpaBucketDao.java

public BucketCollaborator findCollaborator(Long bucketId, Long accountId) {
    String sql = "FROM BucketCollaborator bc " + "WHERE bc.account.id = :accountId "
            + "AND bc.bucket.id =:bucketId";

    Query query = this.em.createQuery(sql);
    query.setParameter("accountId", accountId);
    query.setParameter("bucketId", bucketId);

    BucketCollaborator collaborator = null;
    try {/* w  w  w  . j  ava2s .c  om*/
        collaborator = (BucketCollaborator) query.getSingleResult();
    } catch (Exception e) {
        // Do nothing;
    }

    return collaborator;
}