Example usage for javax.persistence TypedQuery getSingleResult

List of usage examples for javax.persistence TypedQuery getSingleResult

Introduction

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

Prototype

X getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single result.

Usage

From source file:com.siriusit.spezg.multilib.storage.jpa.JpaMessageRepository.java

@Override
public long countMessagesByViewedState(Person receiver, boolean viewedState) {
    TypedQuery<Long> query = entityManager.createNamedQuery("findMessageByViewedState", Long.class);
    query.setParameter("receiver", receiver);
    query.setParameter("viewed", viewedState);
    return query.getSingleResult();
}

From source file:com.beto.test.securityinterceptor.security.UserPageRoleServiceImpl.java

@Override
public List<SecPageRoleDef> findUrlByUrl(String url) throws Exception {

    List<SecPageRoleDef> pageRoles = null;
    try {/*from   w w w .  j  a  v  a2 s.co  m*/
        url = StringUtils.substringBefore(url, "?");
        logger.debug("findUrlByUrl method is called..." + url);
        TypedQuery<SecPageDef> createQuery = em.createNamedQuery("SecPageDef.findByViewId", SecPageDef.class);
        createQuery.setParameter("viewId", url);
        pageRoles = createQuery.getSingleResult().getSecPageRoleDefList();

        for (SecPageRoleDef pageRole : pageRoles) {
            logger.debug(pageRole.toString());
        }
    } catch (NoResultException e) {
        // TODO: handle exception
    }

    return pageRoles;
}

From source file:org.osiam.resource_server.storage.dao.ResourceDao.java

public <T extends ResourceEntity, V> boolean isUniqueAttributeAlreadyTaken(String attributeValue, String id,
        SingularAttribute<? super T, V> attribute, Class<T> clazz) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> cq = cb.createQuery(Long.class);
    Root<T> resource = cq.from(clazz);

    cq.select(cb.countDistinct(resource));

    Predicate predicate = cb.equal(resource.get(attribute), attributeValue);
    if (id != null) {
        Predicate ignoreId = cb.notEqual(resource.get(ResourceEntity_.id), id);
        predicate = cb.and(predicate, ignoreId);
    }// ww w . ja  v  a  2 s  .c om
    cq.where(predicate);

    TypedQuery<Long> countQuery = em.createQuery(cq);

    return countQuery.getSingleResult() > 0;
}

From source file:eu.domibus.common.dao.MessageLogDao.java

public long countEntries() {
    final TypedQuery<Long> query = this.em.createNamedQuery("MessageLogEntry.countEntries", Long.class);
    return query.getSingleResult();
}

From source file:com.hartveld.commons.db.DAOBase.java

@Override
public T retrieveById(final long id) {
    LOG.trace("retrieveById: {}", id);

    final TypedQuery<T> query = em.createQuery("from " + entityName + " where id = :id", entityClass);

    query.setParameter("id", id);

    return query.getSingleResult();
}

From source file:com.epam.training.taranovski.web.project.repository.implementation.UserRepositoryImplementation.java

@Override
public boolean nameExistsInDB(String name) {
    EntityManager em = entityManagerFactory.createEntityManager();
    boolean exists = false;

    try {/*from ww  w .  j a va2 s  .c  om*/
        em.getTransaction().begin();

        TypedQuery<User> query = em.createNamedQuery("User.findByLogin", User.class);
        query.setParameter("login", name);

        query.getSingleResult();

        em.getTransaction().commit();
        exists = true;
    } catch (NoResultException ex) {
        Logger.getLogger(OfferBidRepositoryImplementation.class.getName()).info(ex);
        exists = false;
    } finally {
        if (em.getTransaction().isActive()) {
            em.getTransaction().rollback();
        }
        em.close();
    }

    return exists;
}

From source file:aka.pirana.springsecurity.dao.JpaUserDaoImpl.java

@Override
public User login(String email, String password) {
    System.out.println("aka.pirana.springsecurity.dao.JpaUserDaoImpl.login(" + email + "," + password + ")");
    TypedQuery<User> query = em.createQuery("select u from users u where u.email=?1 and u.password=?2",
            User.class);
    query.setParameter(1, email);/*  ww  w .j  ava  2  s.  c o m*/
    query.setParameter(2, password);
    try {
        return query.getSingleResult();
    } catch (NonUniqueResultException | NoResultException e) {
        return null;
    }

}

From source file:com.ewcms.content.particular.dao.FrontEmployeBasicDAO.java

public EmployeBasic findEmployeBasicByCardCode(final String cardCode) {
    String hql = "From EmployeBasic As p Where p.cardCode=:cardCode";
    TypedQuery<EmployeBasic> query = this.getEntityManager().createQuery(hql, EmployeBasic.class);
    query.setParameter("cardCode", cardCode);
    EmployeBasic employeBasic = null;/*from w w  w  . j av  a2 s . co  m*/
    try {
        employeBasic = (EmployeBasic) query.getSingleResult();
    } catch (NoResultException e) {
    }
    return employeBasic;
}

From source file:com.ewcms.content.particular.dao.FrontProjectBasicDAO.java

public ProjectBasic findProjectBasicByCode(final String code) {
    String hql = "From ProjectBasic As p Where p.code=:code and p.release=true";
    TypedQuery<ProjectBasic> query = this.getEntityManager().createQuery(hql, ProjectBasic.class);
    query.setParameter("code", code);
    ProjectBasic projectBasic = null;//from   w  ww.  j ava2 s . co  m
    try {
        projectBasic = (ProjectBasic) query.getSingleResult();
    } catch (NoResultException e) {
    }
    return projectBasic;
}

From source file:org.osiam.resource_server.storage.dao.ResourceDao.java

/**
 * Retrieves a single {@link ResourceEntity} by the given attribute and value.
 * // w  ww.ja va2  s .c om
 * @param attribute
 *        The attribute of the resource entity to retrieve it by
 * @param value
 *        The value of the attribute to compare it to
 * @param clazz
 *        The concrete resource entity class to retrieve (may also be {@link ResourceEntity})
 * @return The matching {@link ResourceEntity}
 * @throws ResourceNotFoundException
 *         If no {@link ResourceEntity} could be found
 * @throws OsiamException
 *         If more than 1 {@link ResourceEntity} was found
 */
public <T extends ResourceEntity, V> T getByAttribute(SingularAttribute<? super T, V> attribute, V value,
        Class<T> clazz) {

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(clazz);
    Root<T> resource = cq.from(clazz);

    cq.select(resource).where(cb.equal(resource.get(attribute), value));

    TypedQuery<T> q = em.createQuery(cq);

    try {
        return q.getSingleResult();
    } catch (NoResultException nre) {
        throw new ResourceNotFoundException(
                String.format("Resource with attribute '%s' set to '%s' not found", attribute.getName(), value),
                nre);
    } catch (NonUniqueResultException nure) {
        throw new OsiamException(String.format("Muliple resources with attribute '%s' set to '%s' found",
                attribute.getName(), value), nure);
    }
}