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:nl.npcf.eav.store.EntityAttributeValueStore.java

@Transactional
public EAVEntity findByKey(String key) {
    checkInitialized();/*from w w  w  . j ava2 s .c  o m*/
    Query query = entityManager.createQuery("select entity from EAVEntity entity where entity.key = :key");
    query.setParameter("key", key);
    EAVEntity entity = (EAVEntity) query.getSingleResult();
    entity.setSchema(getSchema());
    entity.sortValues();
    return entity;
}

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

/**
 * Deletes only form with empty questions, otherwise throws NoResultException exception.
 * @param formId Long//from   ww w . j a  va 2  s  .c o  m
 */
public void deleteFormWithEmptyQuestions(Long formId) {
    //prevent from deleting a QuestionnaireForm item that has questions.
    //This scenario may appear by editing the URL.
    String jpql = "select f from BaseForm f left join f.elements q " + "where f.id = :formId and q is null";
    Query query = em.createQuery(jpql);
    query.setParameter("formId", formId);
    try {
        BaseForm form = (BaseForm) query.getSingleResult();
        delete(form);
    } catch (NoResultException e) {
        logger.info("try to delete an not empty form");
    }
}

From source file:it.scoppelletti.programmerpower.wui.WebApplicationRegistry.java

/**
 * Legge un’applicazione Web./*from   w ww .j  av a2 s.  c o m*/
 * 
 * @param  ctxPath Contesto dell’applicazione.
 * @return         Oggetto. Se l’applicazione non esiste, restituisce
 *                 {@code null}.
 */
private WebApplication loadApplication(String ctxPath) {
    Query query;
    WebApplication appl;

    query = myEntityMgr.createQuery("from WebApplication where contextPath = :contextPath")
            .setParameter("contextPath", ctxPath);
    try {
        appl = (WebApplication) query.getSingleResult();
    } catch (NoResultException ex) {
        return null;
    }

    return appl;
}

From source file:co.sip.dmesmobile.bs.ScPersonDao.java

@Override
@Transactional/* w ww . ja va2 s  . c o  m*/
public ScEmployee getScEmployeeById(long id) {
    entityManager = Factory.getEntityManagerFactory().createEntityManager();
    ScEmployee result = null;
    try {
        Query query = entityManager.createNamedQuery("ScEmployee.findByIdPerson");
        query.setParameter("idPerson", id);
        result = (ScEmployee) query.getSingleResult();
    } catch (Exception e) {
        log.error("Error al intentar consultar un empleado por ID", e);
        throw e;

    }

    return result;
}

From source file:co.sip.dmesmobile.bs.ScPersonDao.java

@Override
public ScMachine getScMachineById(long idMachine) {
    entityManager = Factory.getEntityManagerFactory().createEntityManager();

    ScMachine result = null;/*from ww w  .  j  av a 2  s.  co  m*/
    try {

        Query query = entityManager.createNamedQuery("ScMachine.findById");
        query.setParameter("idMachine", idMachine);
        result = (ScMachine) query.getSingleResult();
    } catch (Exception e) {
        log.error("Error al intentar consultar todos las mquinas", e);
        throw e;

    }
    return result;
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.RoleDAOJPAImpl.java

@Override
public void updateRole(String name, Role role) {
    Query query = null;
    query = em.createQuery("select r from Role r where r.name=:name");
    query.setParameter("name", name);

    //@SuppressWarnings("rawtypes")
    RoleEntity roleEntity = (RoleEntity) query.getSingleResult();

    domain2entity(role, roleEntity);//from   w ww . j a v a 2 s  .  c  om

    em.persist(roleEntity);

    LOG.debug("Role '{}' updated", role.getName());
}

From source file:DAO.PersonnesStatutsDAOImpl.java

@Transactional(readOnly = true)
@Override//from  w  ww. j a  v  a 2 s  .  com
public PersonnesStatutsEntity find(PersonnesEntity p, StatutsEntity s) {
    Query q = em.createQuery(
            "SELECT ps FROM PersonnesStatutsEntity ps WHERE ps.personne.id = ? AND ps.statut.id = ?");
    q.setParameter(1, p.getId());
    q.setParameter(2, s.getId());
    try {
        return (PersonnesStatutsEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:es.ucm.fdi.dalgs.degree.repository.DegreeRepository.java

public Integer numberOfPages(Boolean showAll) {
    Query query = null;
    if (showAll)//from  ww w  .j  a v a 2s .co  m
        query = em.createNativeQuery("select count(*) from degree");
    else
        query = em.createNativeQuery("select count(*) from degree 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:com.healthcit.cacure.dao.FormDao.java

public boolean areAllModuleFormsApproved(Long moduleId) {
    long approvedCount = 0;
    long totalCount = Long.MAX_VALUE;

    // Get the all forms count
    String totalJpql = "select count(distinct frm) from QuestionnaireForm frm "
            + "where frm.module.id = :moduleId";
    Query totalQuery = em.createQuery(totalJpql);
    totalQuery.setParameter("moduleId", moduleId);
    totalCount = (Long) totalQuery.getSingleResult();

    // Get the returned forms count
    String approvedJpql = "select count(distinct frm) from QuestionnaireForm frm "
            + "where frm.module.id = :moduleId and frm.status = :status";
    Query approvedQuery = em.createQuery(approvedJpql);
    approvedQuery.setParameter("moduleId", moduleId);
    approvedQuery.setParameter("status", FormStatus.APPROVED);
    approvedCount = (Long) approvedQuery.getSingleResult();

    // If there are no forms there, they cannot be all approved
    if (totalCount == 0) {
        return false;
    }/*ww  w  .  j  a  va  2  s . co m*/

    boolean allFormsApproved = totalCount == approvedCount;
    return allFormsApproved;
}

From source file:de.jaxenter.eesummit.caroline.backend.impl.UserServiceImpl.java

@Override
public CaroLineUser login(String loginId, String password) {
    Validate.notNull(loginId);/*from   ww  w  . j a  va2s  .  c o  m*/
    Validate.notNull(password);

    String pwdHash = getPasswordHash(password);

    Query q = em.createQuery(
            "select u from CaroLineUser as u " + "where u.loginId = :loginId and u.loginHash = :loginHash",
            CaroLineUser.class);
    q.setParameter("loginId", loginId);
    q.setParameter("loginHash", pwdHash);

    CaroLineUser usr = null;

    try {
        usr = (CaroLineUser) q.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }

    return usr;
}