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:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public Integer moveMailsToFolder(List<Long> privateMessageIds, Long privateMessageFolderId) {
    try {/*from w  w w  . ja  va 2 s  .c o m*/

        String hql = "UPDATE PrivateMessages c "
                + "SET c.privateMessageFolderId = :privateMessageFolderId, c.isTrash = false "
                + "where c.privateMessageId IN (:privateMessageIds) ";

        Query query = em.createQuery(hql);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);
        query.setParameter("privateMessageIds", privateMessageIds);
        int updatedEntities = query.executeUpdate();

        //Refresh the Entities in the Cache as Hibernate will not do it!
        //FIXME weird code
        for (Long privateMessageId : privateMessageIds) {
            String hqlSel = "select c from PrivateMessages c "
                    + "where c.privateMessageId = :privateMessageId ";

            TypedQuery<PrivateMessages> querySel = em.createQuery(hqlSel, PrivateMessages.class);
            querySel.setParameter("privateMessageId", privateMessageId);

            try {
                querySel.getSingleResult();
            } catch (NoResultException ex) {
            }
        }
        return updatedEntities;
    } catch (Exception e) {
        log.error("[updatePrivateMessagesReadStatus]", e);
    }
    return -1;
}

From source file:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public int updatePrivateMessagesToTrash(List<Long> privateMessageIds, Boolean isTrash,
        Long privateMessageFolderId) {
    try {//from w  w w . ja  v  a  2 s. co  m

        String hql = "UPDATE PrivateMessages c "
                + "SET c.isTrash = :isTrash,c.privateMessageFolderId = :privateMessageFolderId "
                + "where c.privateMessageId IN (:privateMessageIds) ";

        Query query = em.createQuery(hql);
        query.setParameter("isTrash", isTrash);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);
        query.setParameter("privateMessageIds", privateMessageIds);
        int updatedEntities = query.executeUpdate();

        //Refresh the Entities in the Cache as Hibernate will not do it!
        //FIXME weird code
        for (Long privateMessageId : privateMessageIds) {
            String hqlSel = "select c from PrivateMessages c "
                    + "where c.privateMessageId = :privateMessageId ";

            TypedQuery<PrivateMessages> querySel = em.createQuery(hqlSel, PrivateMessages.class);
            querySel.setParameter("privateMessageId", privateMessageId);

            try {
                querySel.getSingleResult();
            } catch (NoResultException ex) {
            }
        }

        return updatedEntities;
    } catch (Exception e) {
        log.error("[updatePrivateMessagesToTrash]", e);
    }
    return -1;
}

From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java

/**
 * {@inheritDoc}//w w  w.  j  a  va2 s. c o m
 */
@Override
public <T> T getScalarResultOfType(Class<T> resultClass, QueryType type, String query, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        switch (type) {
        case JPA:
            TypedQuery<T> typedQuery = em.createQuery(query, resultClass);
            JpaUtil.bindQueryParams(typedQuery, posParams);
            return typedQuery.getSingleResult();
        case NATIVE:
            Query nativeQuery = em.createNativeQuery(query, resultClass);
            JpaUtil.bindQueryParams(nativeQuery, posParams);
            return resultClass.cast(nativeQuery.getSingleResult());
        default:
            throw new IllegalStateException("Invalid QueryType!!");
        }
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

From source file:org.openmeetings.app.data.basic.Fieldmanagment.java

private Fieldlanguagesvalues getFieldlanguagesvaluesById(Long fieldlanguagesvalues_id) throws Exception {
    String hql = "select f from Fieldlanguagesvalues f WHERE f.fieldlanguagesvalues_id = :fieldlanguagesvalues_id ";
    TypedQuery<Fieldlanguagesvalues> query = em.createQuery(hql, Fieldlanguagesvalues.class);
    query.setParameter("fieldlanguagesvalues_id", fieldlanguagesvalues_id);
    Fieldlanguagesvalues flv = null;//from  w  ww  .j  av  a 2 s.  com
    try {
        flv = performReplace(query.getSingleResult());
    } catch (NoResultException ex) {
    }
    return flv;
}

From source file:com.music.dao.PieceDao.java

public PieceEvaluation getEvaluation(long pieceId, Long userId, String ip) {
    TypedQuery<PieceEvaluation> query;
    if (userId != null) {
        query = getEntityManager().createQuery(
                "SELECT ev FROM PieceEvaluation ev where ev.piece.id=:pieceId AND ev.user.id=:userId",
                PieceEvaluation.class);
        query.setParameter("userId", userId);
    } else {/*from ww  w.j  a  va2  s.co m*/
        query = getEntityManager().createQuery(
                "SELECT ev FROM PieceEvaluation ev where ev.piece.id=:pieceId AND ev.ip=:ip",
                PieceEvaluation.class);
        query.setParameter("ip", ip);
    }
    query.setParameter("pieceId", pieceId);

    try {
        return query.getSingleResult();
    } catch (NoResultException ex) {
        return null;
    }
}

From source file:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public PrivateMessages getPrivateMessagesById(Long privateMessageId) {
    try {//from  www. j  a va 2 s  .  c o  m

        String hql = "select c from PrivateMessages c " + "where c.privateMessageId = :privateMessageId ";

        TypedQuery<PrivateMessages> query = em.createQuery(hql, PrivateMessages.class);
        query.setParameter("privateMessageId", privateMessageId);

        PrivateMessages privateMessage = null;
        try {
            privateMessage = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return privateMessage;

    } catch (Exception e) {
        log.error("[countPrivateMessagesByUser]", e);
    }
    return null;
}

From source file:org.jresponder.dao.MainDao.java

/**
 * Get the next Subscription record where the next_send_date is greater
 * than or equal to now and it has one of the statuses provided.  Passing
 * no statuses will result in that restriction not being applied to the
 * query (i.e. all statuses are considered). 
 * //from   ww w .  j ava  2 s.c om
 * @param aSubscriptionStatusArray
 * @return
 */
@Transactional(readOnly = true)
public Subscription getNextSendableSubscription(SubscriptionStatus... aSubscriptionStatusArray) {

    String myStatusSuffix = aSubscriptionStatusArray != null && aSubscriptionStatusArray.length > 0
            ? " AND sion.status IN :statuses "
            : "";

    TypedQuery<Subscription> q = em
            .createQuery("SELECT sion FROM Subscription sion WHERE " + "sion.nextSendDate < :d AND "
                    + "sion.nextSendDate IS NOT NULL " + myStatusSuffix, Subscription.class)
            .setParameter("d", new Date()).setMaxResults(1);

    if (myStatusSuffix.length() > 0) {
        List<String> myStringList = new ArrayList<String>();
        for (SubscriptionStatus s : aSubscriptionStatusArray) {
            myStringList.add(s.toString());
        }
        q.setParameter("statuses", myStringList);
    }

    try {
        return q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }

}

From source file:org.openmeetings.app.data.user.dao.PrivateMessagesDaoImpl.java

public Long getNumberMessages(Long toUserId, Long privateMessageFolderId, boolean isRead) {
    try {//from w w  w.j av  a 2 s.c om

        String hql = "select COUNT(c.privateMessageId) from PrivateMessages c "
                + "where c.to.user_id = :toUserId " + "AND c.isTrash = :isTrash "
                + "AND c.owner.user_id = :toUserId " + "AND c.isRead = :isRead "
                + "AND c.privateMessageFolderId = :privateMessageFolderId ";

        TypedQuery<Long> query = em.createQuery(hql, Long.class);
        query.setParameter("toUserId", toUserId);
        query.setParameter("isTrash", false);
        query.setParameter("isRead", false);
        query.setParameter("privateMessageFolderId", privateMessageFolderId);
        return query.getSingleResult();

    } catch (Exception e) {
        log.error("[getNumberMessages]", e);
    }
    return null;
}

From source file:net.navasoft.madcoin.backend.model.controller.impl.WorkRequestDataAccess.java

/**
 * Find last./*  w w  w  .j  a  va  2  s  .co  m*/
 * 
 * @param dbAccess
 *            the db access
 * @param target
 *            the target
 * @param newRecord
 *            the new record
 * @return the work requests
 * @since 2/09/2014, 09:31:40 PM
 */
@Override
protected WorkRequests findLast(EntityManager dbAccess, Class<WorkRequests> target, WorkRequests newRecord) {
    TypedQuery<WorkRequests> query = entityManager
            .createNamedQuery("WorkRequests." + NormalizedEntity.NAMED_QUERY_ID, target);
    query.setParameter("categoryId", newRecord.getWorkRequestsPK().getCategoryId());
    return query.getSingleResult();
}