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:es.ucm.fdi.dalgs.module.repository.ModuleRepository.java

public Module existByCode(String code, Long id_degree) {
    Degree d = em.getReference(Degree.class, id_degree);
    Query query = em.createQuery("select m from Module m where m.info.code=?1 and m.degree = ?2");
    query.setParameter(1, code);//w w  w  .j a v a 2s. c o  m
    query.setParameter(2, d);
    if (query.getResultList().isEmpty())
        return null;
    else
        return (Module) query.getSingleResult();
}

From source file:es.ucm.fdi.dalgs.subject.repository.SubjectRepository.java

public Subject getSubjectForCourse(Long id_course) {
    Course course = em.getReference(Course.class, id_course);

    Query query = em.createQuery("select s from Subject s join s.courses c where c=?1");
    query.setParameter(1, course);/*w w w. j av a2 s  . com*/

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

    return (Subject) query.getSingleResult();
}

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

public Preference findPreference(String username, String shortName) {
    Query query = getEntityManager().createNamedQuery("Preference.findPreferenceWithUsernameAndShortname");
    query.setParameter("username", username);
    query.setParameter("shortName", shortName);
    Preference result;//from   ww  w  . j a  v a  2  s  .  c  om
    try {
        result = (Preference) query.getSingleResult();
    } catch (Exception e) {
        LOG.info("No Preference Found: " + username + ", " + shortName);
        result = null;
    }
    return result;
}

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

/**
 * {@inheritDoc}//w  w  w.ja v  a2 s  . c o m
 */
@Override
public Certificate findCertificate(int certificateID) {
    Query query = entityManager.createQuery("SELECT c from Certificate c WHERE id=:id");
    //query.setParameter(1, issuer);
    query.setParameter("id", certificateID);

    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" + certificateID);
    }
}

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

/**
 * @param formId Long//from w w w.  j ava2 s.c o  m
 * @param uuid Long
 * @return true if question exists in form
 */
public boolean isQuestionAlreadyExistsInForm(Long formId, String uuid) {
    //      String jpql = "select count(*) from BaseQuestion q , FormElement e where q.parent.id = e.id and e.form.id = :formId and (q.uuid = :uuid or q.linkId = :uuid) and linkId is not null";
    /* Only questions that are imported wil be links */
    String sql = "select count(*) from form_element e where e.form_id = :formId and e.link_id = :uuid";
    //Query query = em.createQuery(jpql);
    Query query = em.createNativeQuery(sql);
    query.setParameter("formId", formId);
    query.setParameter("uuid", uuid);
    int count = Integer.valueOf(query.getSingleResult().toString());
    return count > 0;
}

From source file:com.assetmanager.service.auth.PersistentTokenRepositoryImpl.java

/**
 * Removes the tokens for the given username.
 *
 * @param username the username/*  w w w  . ja v  a2 s.c o  m*/
 */
@Override
@Transactional
public final void removeUserTokens(final String username) {
    final Query query = entityManager.createQuery("SELECT p FROM PersistentUser p WHERE username = :username");
    query.setParameter("username", username);

    if (query.getResultList().size() > 0) {
        final PersistentUser persistentUser = (PersistentUser) query.getSingleResult();
        entityManager.remove(persistentUser);
    }
}

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

public boolean isFormWithTheSameNameExistInLibrary(final String formName) {
    Query q = this.em.createQuery("SELECT count(form.id) FROM FormLibraryForm form WHERE form.name = :name");
    q.setParameter("name", formName);
    long count = (Long) q.getSingleResult();
    return count > 0;
}

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

public long getSkipTriggerQuestionsCount(Long formId) {
    Query q = this.em.createQuery("SELECT count(asr.id) FROM AnswerSkipRule asr WHERE asr.formId = :formId");
    q.setParameter("formId", formId);
    long count = (Long) q.getSingleResult();
    return count;
}

From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceTypeDomainService.java

private ResourceTypeEntity getUniqueResourceTypeByName(String resourceTypeName) {
    ResourceTypeEntity resourceType = null;
    try {/*from  w ww .  ja v a2s.c  om*/
        Query searchUniqueResourceTypeQuery = queries.searchResourceTypeByNameCaseInsensitive(resourceTypeName);
        resourceType = (ResourceTypeEntity) searchUniqueResourceTypeQuery.getSingleResult();
    } catch (NoResultException nre) {
        String message = "Der ResourceType: " + resourceTypeName + " existiert nicht auf der DB";
        log.info(message);
    }

    return resourceType;
}

From source file:co.edu.uniandes.csw.uniandes.sport.service._SportService.java

@GET
public Response getSports(@Context HttpHeaders httpHeaders, @QueryParam("page") Integer page,
        @QueryParam("maxRecords") Integer maxRecords) {
    //      Imprime el header que ha enviado el usuario desde el cliente
    try {/*from w  w w .  j  a  va2s  . co m*/

        Subject currentUser = SecurityUtils.getSubject();
        Usuario user = (Usuario) currentUser.getPrincipal();
        String tenant = user.getTenant();
        Map<String, Object> emProperties = new HashMap<String, Object>();
        emProperties.put("eclipselink.tenant-id", tenant);//Asigna un valor al multitenant
        entityManager = PersistenceManager.getInstance().getEntityManagerFactory()
                .createEntityManager(emProperties);

        Query count = entityManager.createQuery("select count(u) from SportEntity u");
        Long regCount = 0L;
        regCount = Long.parseLong(count.getSingleResult().toString());
        Query q = entityManager.createQuery("select u from SportEntity u");
        if (page != null && maxRecords != null) {
            q.setFirstResult((page - 1) * maxRecords);
            q.setMaxResults(maxRecords);
        }
        SportPageDTO response = new SportPageDTO();
        response.setTotalRecords(regCount);
        response.setRecords(SportConverter.entity2PersistenceDTOList(q.getResultList()));
        String json = new Gson().toJson(response);
        return Response.status(200).header("Access-Control-Allow-Origin", "*").entity(json).build();

    } catch (Exception e) {
        e.printStackTrace();
        return Response.status(401).header("Access-Control-Allow-Origin", "*").entity("You need Authenticated")
                .build();
    }
}