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:ext.msg.model.Message.java

public static Page<Message> queryMessageByRead(int page, int pageSize, String consumeOnly,
        Collection<?> msgTypes, boolean unReadFirst) {
    String countQL = " select count(m) from Message m where m.consumeOnly = :consumeOnly ";
    String contentQL = " from Message m where m.consumeOnly = :consumeOnly ";

    if (CollectionUtils.isNotEmpty(msgTypes)) {
        countQL += "  and m.msgType in (:msgTypes) ";
        contentQL += "  and m.msgType in (:msgTypes)  ";
    }/*from   w w  w  .ja va  2  s .c om*/

    if (unReadFirst) {
        contentQL += " order by isRead asc, m.id desc ";
    } else {
        contentQL += " order by m.id desc ";
    }

    TypedQuery<Long> countQuery = JPA.em().createQuery(countQL, Long.class).setParameter("consumeOnly",
            consumeOnly);
    TypedQuery<Message> contentQuery = JPA.em().createQuery(contentQL, Message.class)
            .setParameter("consumeOnly", consumeOnly);

    if (CollectionUtils.isNotEmpty(msgTypes)) {
        countQuery.setParameter("msgTypes", msgTypes);
        contentQuery.setParameter("msgTypes", msgTypes);
    }

    contentQuery.setFirstResult(page * pageSize).setMaxResults(pageSize);

    Long total = countQuery.getSingleResult();
    List<Message> data = contentQuery.getResultList();
    Page<Message> pageMsg = new Page<Message>(Constants.SUCESS, total, data);
    return pageMsg;
}

From source file:com.web.mavenproject6.service.PersonalServiceImp.java

@Override
public Object findByAccessNumber(String accessNumber) {
    TypedQuery query = em.createQuery("select p from personal p where p.accessNumber = ?1", personal.class);
    query.setParameter(1, accessNumber);

    try {/* w  w w  . java  2  s. c om*/
        return query.getSingleResult();
    } catch (Exception ee) {
        return false;
    }
}

From source file:nc.noumea.mairie.organigramme.core.dao.PersistentManager.java

public T findSingleByProperty(Class<? extends T> classe, String property, Object value) {
    TypedQuery<T> q = constructTypedQueryByPropertyOrderBy(classe, property, value, null);
    return q.getSingleResult();
}

From source file:com.beto.test.securityinterceptor.model.dao.generic.AbstractDao.java

public T getObWithNamedQuery(String query, String paramName, Object param) throws Exception {
    TypedQuery<T> tq = getEntityManager().createNamedQuery(query, entityClass);
    tq.setParameter(paramName, param);/*  w  w w  .  j a  va 2 s.c  o  m*/
    return tq.getSingleResult();
}

From source file:com.home.ln_spring.ch10.service.jpa.ContactServiceImplOld.java

@Override
@Transactional(readOnly = true)/*  w  w w.j a v  a  2  s  .  c  om*/
public Contact findById(Long id) {
    TypedQuery<Contact> query = em.createNamedQuery("Contact.findById", Contact.class);
    query.setParameter("id", id);
    return query.getSingleResult();
}

From source file:com.jcs.goboax.aulavirtual.dao.impl.UsuarioDaoImpl.java

@Override
public Usuario findByEmail(String anEmail) {
    TypedQuery<Usuario> myQuery = entityManager.createNamedQuery(Usuario.USUARIO_BY_EMAIL, entityClass);
    myQuery.setParameter(Usuario.USUARIO_EMAIL_PARAMETER, anEmail);
    try {//from www .  jav  a2  s  . c  o m
        return myQuery.getSingleResult();
    } catch (NoResultException e) {
        throw new AulaVirtualPersistenceException("No Result for Query", e);
    } catch (NonUniqueResultException e) {
        throw new AulaVirtualPersistenceException("More than one result", e);
    }
}

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

public int getProjectShenPiArticleCount(String channelChildrens) {

    String sql = "Select count(p.id) From ProjectArticle as p where p.channelId in(" + channelChildrens
            + ") and p.release=true ";
    TypedQuery<Long> query = this.getEntityManager().createQuery(sql, Long.class);
    return Integer.parseInt(query.getSingleResult().toString());
}

From source file:net.triptech.metahive.model.Record.java

/**
 * Count the records.//from  w w w .j  a v a 2  s .  c o m
 *
 * @param filter the filter
 * @return the long
 */
public static long countRecords(final RecordFilter filter) {

    StringBuilder sql = new StringBuilder("SELECT COUNT(r) FROM Record r");

    Map<String, Map<String, Object>> whereParameters = buildWhere(filter);
    Map<String, Object> variables = new HashMap<String, Object>();

    if (whereParameters.size() > 0) {
        String sqlWhere = whereParameters.keySet().iterator().next();
        variables = whereParameters.get(sqlWhere);

        sql.append(sqlWhere);
    }

    TypedQuery<Long> q = entityManager().createQuery(sql.toString(), Long.class);

    for (String variable : variables.keySet()) {
        q.setParameter(variable, variables.get(variable));
    }

    return q.getSingleResult();
}

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param ISBN/*  w ww .  j av  a2s.  c o  m*/
 *            the ISBN of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookByISBN(final String ISBN) {
    BookType m = null;
    if (ISBN == null) {
        this.logger.debug("getBookByISBN: ISBN is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(BookType_.ISBN), ISBN));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for ISBN=" + ISBN);
            m = null;
        }
    }
    return m;
}

From source file:com.samples.platform.serviceprovider.library.internal.dao.PlatformDao.java

/**
 * @param uuid/*  w w  w .j av a  2 s.co m*/
 *            the id of the book to find.
 * @return the {@link BookType}.
 */
@Transactional(value = PersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public BookType getBookById(final String uuid) {
    BookType m = null;
    if (uuid == null) {
        this.logger.debug("getBookById: UUID is null.");
    } else {
        CriteriaBuilder cb = this.em.getCriteriaBuilder();
        CriteriaQuery<BookType> q = cb.createQuery(BookType.class);
        Root<BookType> c = q.from(BookType.class);
        q.where(cb.equal(c.<String>get(EntityType_.UUID), uuid));
        TypedQuery<BookType> typedQuery = this.em.createQuery(q);
        try {
            m = typedQuery.getSingleResult();
            this.logger.debug("getBookByISBN: " + ToStringBuilder.reflectionToString(m));
        } catch (NoResultException e) {
            this.logger.debug("getBookByISBN: non value found for id=" + uuid);
            m = null;
        }
    }
    return m;
}