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.bizintelapps.bugtracker.dao.impl.GenericDaoImpl.java

protected Object executeNamedQueryReturnSingleObject(final String namedQuery, final Object... params)
        throws DataAccessException {
    Query query = entityManager.createNamedQuery(namedQuery);
    int i = 1;//from w w w.  j a va  2  s .c om
    for (Object param : params) {
        query.setParameter(i++, param);
    }
    try {
        return query.getSingleResult();
    } catch (RuntimeException re) {
        log.debug(re);
        return null;
    }
}

From source file:es.ucm.fdi.dalgs.academicTerm.repository.AcademicTermRepository.java

public Integer numberOfPages(Boolean showAll) {

    Query query = null;
    if (showAll)// w  w w.j  a  v  a  2 s.  c  o  m
        query = em.createNativeQuery("select count(*) from academicterm");
    else
        query = em.createNativeQuery("select count(*) from academicterm where isDeleted='false'");

    logger.info(query.getSingleResult().toString());
    double dou = Double.parseDouble(query.getSingleResult().toString()) / ((double) noOfRecords);
    return (int) Math.ceil(dou);

}

From source file:corner.orm.gae.impl.PaginatedJpaEntityServiceTest.java

@Test
public void test_count() {
    EntityManager entityManager = newMock(EntityManager.class);
    Query query = newMock(Query.class);
    expect(entityManager//from  ww  w .j a  v a2s .c om
            .createQuery("select count(root)  from corner.orm.gae.impl.TestAEntity as root  where name=?"))
                    .andReturn(query);
    expect(query.setParameter(0, "acai")).andReturn(query);
    expect(query.getSingleResult()).andReturn(new Integer(1234));
    JpaTemplate jpaTemplate = GaeModule.buildJpaTemplate(entityManager);
    replay();
    PaginatedJapEntityService pjes = new PaginatedJapEntityService(jpaTemplate, typeCoercer);
    assertEquals(1234, pjes.count(TestAEntity.class, new String[] { "name=?", "acai" }));
    verify();
}

From source file:es.ucm.fdi.dalgs.module.repository.ModuleRepository.java

public Module getModule(Long id, Long id_degree) {

    Degree degree = em.getReference(Degree.class, id_degree);
    Query query = em.createQuery("select m from Module m where m.id=?1 and m.degree=?2  ");
    query.setParameter(1, id);//from   w  ww  .  j av a2  s  .  c  om
    query.setParameter(2, degree);

    if (query.getResultList().isEmpty())
        return null;

    return (Module) query.getSingleResult();
}

From source file:be.fedict.trust.service.dao.bean.CertificateAuthorityDAOBean.java

public BigInteger findCrlNumber(String issuerName) {
    LOG.debug("get CRL number for " + issuerName);
    Query query = this.entityManager.createNamedQuery(RevokedCertificateEntity.QUERY_CRL_NUMBER_WHERE_ISSUER);
    query.setParameter("issuer", issuerName);
    try {//  ww  w  .  java 2 s .co  m
        return (BigInteger) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

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

public BaseForm getByUuid(String uuid) {
    BaseForm form = null;/*from  w ww. j  av  a2s  .c o m*/
    Query query = em.createQuery("from BaseForm fe where uuid = :uuid");
    query.setParameter("uuid", uuid);
    try {
        form = (BaseForm) query.getSingleResult();
    } catch (javax.persistence.NoResultException e) {
        logger.debug("No object found with uuid " + uuid);
    }
    return form;
}

From source file:DAO.PersonnesDAOImpl.java

@Transactional(readOnly = true)
@Override/*from   ww w  . java2  s .c o m*/

public PersonnesEntity find(int id) {
    Query q = em.createQuery("SELECT p FROM PersonnesEntity p where p.id = ?");
    q.setParameter(1, id);
    try {
        return (PersonnesEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:org.syncope.core.persistence.dao.impl.ResourceDAOImpl.java

@Override
public SchemaMapping getMappingForAccountId(final String resourceName) {

    Query query = entityManager.createQuery("SELECT m FROM " + SchemaMapping.class.getSimpleName() + " m "
            + "WHERE m.resource.name=:resourceName " + "AND m.accountid = 1");
    query.setParameter("resourceName", resourceName);

    return (SchemaMapping) query.getSingleResult();
}

From source file:be.fedict.eid.pkira.blm.model.contracts.ContractRepositoryBean.java

/**
* {@inheritDoc}/*from   w  ww . j  a v a  2 s  . c  om*/
*/
@Override
public Certificate findCertificate(String issuer, BigInteger serialNumber) {
    Query query = entityManager.createQuery("SELECT c from Certificate c WHERE serialNumber=:serialNumber");
    query.setParameter("serialNumber", serialNumber);

    try {
        return (Certificate) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (EntityNotFoundException e) {
        return null;
    } catch (NonUniqueResultException e) {
        throw new RuntimeException("Too many results for certificate search " + serialNumber);
    }
}

From source file:es.ucm.fdi.dalgs.learningGoal.repository.LearningGoalRepository.java

public LearningGoal existByCode(String code) {

    Query query = em.createQuery("Select l from LearningGoal l where l.info.code=?1");
    query.setParameter(1, code);/*  ww w. j a  v a  2s .c o  m*/

    if (query.getResultList().isEmpty())
        return null;
    else
        return (LearningGoal) query.getSingleResult();
}