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.jd.survey.dao.security.UserDAOImpl.java

@Transactional
public User findByLogin(String login) throws DataAccessException {
    try {/*  w  w w.  j  a va2 s.  com*/
        Query query = createNamedQuery("User.findByLogin", -1, -1, login);
        return (User) query.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }

}

From source file:com.jd.survey.dao.security.UserDAOImpl.java

@Transactional
public User findByEmail(String email) throws DataAccessException {
    try {/*ww w.ja  va  2  s  .  c om*/
        Query query = createNamedQuery("User.findByEmail", -1, -1, email);
        return (User) query.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }

}

From source file:nc.noumea.mairie.appock.dao.impl.FamilleDaoImpl.java

@Override
public Famille findByLibelleAndCatalogue(String libelle, Catalogue catalogue) {
    // @formatter:off
    String jpaQuery = "   SELECT famille FROM Famille famille "
            + "         left join famille.catalogue catalogue " + "       WHERE famille.libelle = :libelle "
            + "         AND catalogue = :catalogue";
    // @formatter:on

    Query query = persistentManager.getEntityManager().createQuery(jpaQuery);

    AppockUtil.setParameter(query, "libelle", libelle);
    AppockUtil.setParameter(query, "catalogue", catalogue);

    try {//from  w  w  w  .  j  a v  a  2  s.  c  o m
        return (Famille) query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.cimpoint.mes.server.repositories.ContainerRepository.java

public String getNextContainerNumber() throws Exception {
    Query qry = getEntityManager().createQuery("select o from EContainer o order by o.number desc")
            .setMaxResults(1);/*from w w w  . j ava 2  s.  c  o m*/
    try {
        EContainer c = (EContainer) qry.getSingleResult();
        return String.valueOf(Integer.parseInt(c.getNumber()) + 1);
    } catch (NoResultException ex) {
        return this.getInitContainerNumber();
    } catch (Exception ex) {
        ex.printStackTrace();
        throw ex;
    }
}

From source file:com.panguso.lc.analysis.format.dao.impl.DaoImpl.java

@Override
public long findCount() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Long> criteriaQuery = cb.createQuery(Long.class);
    Root<T> customer = criteriaQuery.from(entityClass);
    criteriaQuery.select(cb.count(customer));
    Query query = em.createQuery(criteriaQuery);
    return (Long) query.getSingleResult();
}

From source file:com.exp.tracker.services.impl.JavaMailEmailService.java

/**
 * Sends the settlement email./*from  w  ww. ja v a2 s . co  m*/
 */
@SuppressWarnings("unchecked")
@Async
public void sendSettlementEmail(Long sid, byte[] settlementReport, byte[] expenseReport) {
    // do not send email if sid is not valid
    if (sid != 0l) {
        Query queryGetSettlementForId = em.createNamedQuery("getSettlementForId");
        queryGetSettlementForId.setParameter("id", sid);
        SettlementEntity se = (SettlementEntity) queryGetSettlementForId.getSingleResult();
        SettlementBean sb = new SettlementBean(se);
        //
        Query querygetAllUsers = em.createNamedQuery("getAllUsers");
        List<UserEntity> uel = querygetAllUsers.getResultList();
        List<UserBean> ubl = new ArrayList<UserBean>();
        for (UserEntity ue : uel) {
            if (UserEntity.USER_ENABLED == ue.getEnabled() && null != ue.getEmailId()) {
                if (!("".equalsIgnoreCase(ue.getEmailId()))) {
                    ubl.add(new UserBean(ue));
                }
            }
        }
        // send email
        sendSettlementNotice(sb, ubl, settlementReport, expenseReport);
    }
}

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

public int countContractsByFilter(String userRrn, ContractsFilter contractsFilter) {
    StringBuilder queryString = new StringBuilder();

    queryString.append("SELECT COUNT(contract) FROM ");
    appendFromAndWhere(contractsFilter, queryString);

    Query query = getEntityManager().createQuery(queryString.toString());
    addQueryParameters(userRrn, contractsFilter, query);

    return ((Long) query.getSingleResult()).intValue();
}

From source file:com.solace.data.jpa.GenericDao.java

private E executeSingle(javax.persistence.Query q) throws Exception {

    long now = System.currentTimeMillis();

    E e = (E) q.getSingleResult();

    LOGGER.debug(S_TOOK_D_MS_TO_EXECUTE, q.toString(), System.currentTimeMillis() - now);

    return e;// w ww .j  ava 2  s. c o m
}

From source file:de.inetsource.jsfforum.db.ThreadFacade.java

private Post getPost(Thread t, boolean last) {
    try {//from  www . j a v  a 2  s . c om
        String sort = "asc";
        if (last) {
            sort = "desc";
        }
        Query q = em.createQuery("SELECT p FROM Post p where p.threadId = :thread order by p.created " + sort,
                Post.class);
        q.setParameter("thread", t);
        q.setMaxResults(1);
        return (Post) q.getSingleResult();
    } catch (NoResultException nre) {
        return null;
    }
}

From source file:org.openwms.common.transport.TransportUnitIT.java

public final @Test void testTUwithErrors() throws Exception {
    TransportUnit transportUnit = new TransportUnit(new Barcode("4711"));

    transportUnit.setTransportUnitType(knownType);
    transportUnit.setActualLocation(knownLocation1);
    transportUnit.setTargetLocation(knownLocation1);

    transportUnit.addError(new UnitError());
    Thread.sleep(100);//ww  w.  jav a 2  s .c  o  m
    transportUnit.addError(new UnitError());
    entityManager.persist(transportUnit);

    Query query = entityManager.getEntityManager().createQuery("select count(ue) from UnitError ue",
            Long.class);

    Long cnt = (Long) query.getSingleResult();
    assertThat(cnt).isEqualTo(2);

    entityManager.remove(transportUnit);

    cnt = (Long) query.getSingleResult();
    assertThat(cnt).isEqualTo(0);
}