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.UsersDaoImpl.java

/**
 * //  w  w  w  . java  2  s .c o m
 * @param user_id
 * @return
 */
public Users getUser(Long user_id) {
    if (user_id != null && user_id > 0) {
        try {
            TypedQuery<Users> query = em.createQuery("select c from Users as c where c.user_id = :user_id",
                    Users.class);
            query.setParameter("user_id", user_id);

            Users users = null;
            try {
                users = query.getSingleResult();
            } catch (NoResultException ex) {
            }
            return users;
        } catch (Exception ex2) {
            log.error("getUser", ex2);
        }
    } else {
        log.info("[getUser] " + "Info: No USER_ID given");
    }
    return null;
}

From source file:com.softwarecorporativo.monitoriaifpe.funcionais.atividade.TesteAtividade.java

@Test
public void testeRemoverAtividadesDeMonitoria() {
    Monitoria monitoria = super.entityManager.find(Monitoria.class, 2L);
    Query query = super.entityManager.createQuery("DELETE Atividade AS a WHERE a.monitoria = ?1");
    query.setParameter(1, monitoria);/*from  ww w.  j  a v  a  2  s  .  c  o m*/
    query.executeUpdate();
    TypedQuery<Long> typedQuery = super.entityManager
            .createQuery("SELECT COUNT(a) FROM Atividade a where a.monitoria = :monitoria", Long.class);
    typedQuery.setParameter("monitoria", monitoria);
    Long quantidadeEsperada = 0L;
    Long quantidade = typedQuery.getSingleResult();
    assertEquals(quantidadeEsperada, quantidade);
}

From source file:com.softwarecorporativo.monitoriaifpe.funcionais.atividade.TesteAtividade.java

@Test
public void testeVerificarAtividadeNaMonitoria() {
    TypedQuery<Atividade> queryAtividade = super.entityManager.createQuery(
            "SELECT a FROM Atividade a INNER JOIN FETCH a.monitoria WHERE a.chavePrimaria = ?1",
            Atividade.class);
    queryAtividade.setParameter(1, 1L);/*  w  ww.j a  v a2  s  .  c  o m*/

    Atividade atividade = queryAtividade.getSingleResult();

    TypedQuery<Monitoria> queryMonitoria = super.entityManager
            .createQuery("SELECT m FROM Monitoria m WHERE :item MEMBER OF m.atividades", Monitoria.class);
    queryMonitoria.setParameter("item", atividade);

    Monitoria monitoria = queryMonitoria.getSingleResult();
    assertEquals(atividade.getMonitoria(), monitoria);
}

From source file:eu.domibus.ebms3.common.dao.PModeDao.java

protected String findPartyName(final List<PartyId> partyIds) throws EbMS3Exception {
    Identifier identifier;//from w w w.  j a  v  a 2 s. c  o m
    for (final PartyId partyId : partyIds) {
        try {
            String type = partyId.getType();
            if (type == null || type.isEmpty()) { //PartyId must be an URI
                try {
                    //noinspection ResultOfMethodCallIgnored
                    URI.create(partyId.getValue()); //if not an URI an IllegalArgumentException will be thrown
                    type = "";
                } catch (final IllegalArgumentException e) {
                    final EbMS3Exception ex = new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0003, e,
                            null);
                    ex.setErrorDetail("PartyId " + partyId.getValue() + " is not a valid URI [CORE] 5.2.2.3");
                    throw ex;
                }
            }
            final TypedQuery<Identifier> identifierQuery = this.entityManager
                    .createNamedQuery("Identifier.findByTypeAndPartyId", Identifier.class);
            identifierQuery.setParameter("PARTY_ID", partyId.getValue());
            identifierQuery.setParameter("PARTY_ID_TYPE", type);
            identifier = identifierQuery.getSingleResult();
            final TypedQuery<String> query = this.entityManager.createNamedQuery("Party.findPartyByIdentifier",
                    String.class);
            query.setParameter("PARTY_IDENTIFIER", identifier);

            return query.getSingleResult();
        } catch (final NoResultException e) {
            PModeDao.LOG.debug("", e); // Its ok to not know all identifiers, we just have to know one
        }
    }
    throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0003, "No matching party found", null, null,
            null);
}

From source file:org.openmeetings.app.data.conference.PollManagement.java

public boolean hasVoted(Long room_id, Long userid) {
    try {//from  w  w w  . j av  a  2 s  .  co m
        log.debug(" :: hasVoted :: " + room_id + ", " + userid);
        TypedQuery<RoomPollAnswers> q = em.createQuery("SELECT rpa FROM RoomPollAnswers rpa "
                + "WHERE rpa.roomPoll.room.rooms_id = :room_id AND rpa.votedUser.user_id = :userid AND rpa.roomPoll.archived = :archived",
                RoomPollAnswers.class);
        q.setParameter("room_id", room_id);
        q.setParameter("userid", userid);
        q.setParameter("archived", false);
        q.getSingleResult();
        return true;
    } catch (NoResultException nre) {
        //expected
    } catch (Exception err) {
        log.error("[getPoll]", err);
    }
    return false;
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
public Boolean updateMessageForLock(final Message msg) {
    Assert.notNull(msg, "the msg must not be null");

    // acquire pessimistic lock firstly
    String jSql = "SELECT m " + "FROM " + Message.class.getName() + " m " + "WHERE m.msgId = :msgId"
            + "     AND (m.state = '" + MsgStateEnum.PARTLY_FAILED + "' " + "     OR m.state = '"
            + MsgStateEnum.POSTPONED + "')";

    TypedQuery<Message> q = em.createQuery(jSql, Message.class);
    q.setParameter("msgId", msg.getMsgId());
    // note: https://blogs.oracle.com/carolmcdonald/entry/jpa_2_0_concurrency_and
    q.setLockMode(LockModeType.PESSIMISTIC_WRITE);
    Message dbMsg = q.getSingleResult();

    if (dbMsg != null) {
        // change message's state to PROCESSING
        msg.setState(MsgStateEnum.PROCESSING);
        Date currDate = new Date();
        msg.setStartProcessTimestamp(currDate);
        msg.setLastUpdateTimestamp(currDate);

        update(msg);//w w w  .j av  a2  s .  c om
    }

    return true;
}

From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java

/**
 * Find an entity of the specified class by its unique-key name.
 *//*from   ww  w. j a v a 2 s.co m*/
public <T> T findByUk(String namedQueryName, Map<String, Object> params, Class<T> claz) {
    TypedQuery<T> q = this.getEntityManager().createNamedQuery(namedQueryName, claz);
    Set<String> keys = params.keySet();
    if (IModelWithClientId.class.isAssignableFrom(claz)) {
        q.setParameter("clientId", Session.user.get().getClient().getId());
    }
    for (String key : keys) {
        q.setParameter(key, params.get(key));
    }
    return (T) q.getSingleResult();
}

From source file:org.cleverbus.core.common.dao.MessageDaoJpaImpl.java

@Override
@Nullable//w w  w  .  j a v  a2 s.c  o m
public Message findEagerMessage(Long msgId) {
    Assert.notNull(msgId, "the msgId must not be null");

    TypedQuery<Message> q = em.createQuery("SELECT m FROM " + Message.class.getName() + " m "
            + " left join fetch m.externalCalls  " + " left join fetch m.requests  " + "WHERE m.msgId = :msgId",
            Message.class);
    q.setParameter("msgId", msgId);

    return q.getSingleResult();
}

From source file:com.softwarecorporativo.monitoriaifpe.funcionais.atividade.TesteAtividade.java

@Test
public void testeAprovarAtividadesDeMonitoria() {
    Query query = super.entityManager
            .createQuery("UPDATE Atividade AS a SET a.situacao = ?1 WHERE a.situacao = ?2");
    query.setParameter(1, SituacaoAtividade.APROVADA);
    query.setParameter(2, SituacaoAtividade.AGUARDANDO_APROVACAO);
    query.executeUpdate();/*from  w  ww  .  j  a v  a 2 s  .c  o m*/
    TypedQuery<Long> typedQuery = super.entityManager
            .createQuery("SELECT COUNT(a) FROM Atividade a where a.situacao = :situacao", Long.class);
    typedQuery.setParameter("situacao", SituacaoAtividade.AGUARDANDO_APROVACAO);
    Long quantidadeEsperada = 0L;
    Long quantidade = typedQuery.getSingleResult();
    assertEquals(quantidadeEsperada, quantidade);
}

From source file:org.openmeetings.app.data.file.dao.FileExplorerItemDaoImpl.java

public FileExplorerItem getFileExplorerItemsById(Long fileExplorerItemId) {
    //log.debug(".getFileExplorerItemsById() started");

    try {/*w  w  w.  j a  v a 2s  . co m*/

        String hql = "SELECT c FROM FileExplorerItem c " + "WHERE c.fileExplorerItemId = :fileExplorerItemId";

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

        FileExplorerItem fileExplorerList = null;
        try {
            fileExplorerList = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return fileExplorerList;
    } catch (Exception ex2) {
        log.error("[getFileExplorerItemsById]: ", ex2);
    }
    return null;
}