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.olp.jpa.domain.docu.ut.repo.DepartmentRepositoryImpl.java

@Override
@Transactional(readOnly = true)//from w  w  w. ja va 2s.c o  m
public DepartmentBean findByDeptCode(String deptCode) {

    IContext ctx = ContextManager.getContext();
    String tid = ctx.getTenantId();

    //EntityGraph graph = getEntityManager().getEntityGraph("graph.departmentBean.employees");

    TypedQuery<DepartmentBean> query = getEntityManager().createNamedQuery("DepartmentBean.findByCode",
            DepartmentBean.class);
    query.setParameter("code", deptCode); // TODO: Sanitize input, although low risk due to binding
    query.setParameter("tenant", tid);

    //query.setHint("javax.persistence.fetchgraph", graph); // Entity graph is not supported yet in OGM

    DepartmentBean bean = query.getSingleResult();

    //if (bean != null && bean.getEmployees() != null)
    //    bean.getEmployees().size(); // Apparently no way to avoid loading child entity instances, hence EAGER loading

    return (bean);
}

From source file:org.syncope.core.persistence.dao.impl.ResourceDAOImpl.java

@Override
public ExternalResource find(final String name) {
    TypedQuery<ExternalResource> query = entityManager.createQuery(
            "SELECT e " + "FROM " + ExternalResource.class.getSimpleName() + " e " + "WHERE e.name = :name",
            ExternalResource.class);
    query.setParameter("name", name);

    ExternalResource result = null;//from w ww .  ja  v a  2  s . c  om
    try {
        result = query.getSingleResult();
    } catch (NoResultException e) {
    }

    return result;
}

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

public boolean hasPoll(Long room_id) {
    try {/*  www  .  jav  a  2s  .co m*/
        log.debug(" :: hasPoll :: " + room_id);
        TypedQuery<Long> q = em.createQuery(
                "SELECT COUNT(rp) FROM RoomPoll rp WHERE rp.room.rooms_id = :room_id AND rp.archived = :archived",
                Long.class);
        q.setParameter("room_id", room_id);
        q.setParameter("archived", false);
        return q.getSingleResult() > 0;
    } catch (NoResultException nre) {
        //expected
    } catch (Exception err) {
        log.error("[getPoll]", err);
    }
    return false;
}

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

public RoomPoll getPoll(Long room_id) {
    try {/* w w  w  .jav a  2s  .c om*/
        log.debug(" :: getPoll :: " + room_id);
        TypedQuery<RoomPoll> q = em.createQuery(
                "SELECT rp FROM RoomPoll rp WHERE rp.room.rooms_id = :room_id AND rp.archived = :archived",
                RoomPoll.class);
        q.setParameter("room_id", room_id);
        q.setParameter("archived", false);
        return q.getSingleResult();
    } catch (NoResultException nre) {
        //expected
    } catch (Exception err) {
        log.error("[getPoll]", err);
    }
    return null;
}

From source file:io.hops.hopsworks.common.dao.certificates.CertsFacade.java

public UserCerts findUserCert(String projectName, String username) {
    TypedQuery<UserCerts> query = em.createNamedQuery("UserCerts.findUserProjectCert", UserCerts.class);
    query.setParameter("projectname", projectName);
    query.setParameter("username", username);
    try {// w  w  w  .ja  v  a 2  s  . com
        UserCerts res = query.getSingleResult();
        return res;
    } catch (NoResultException e) {
        Logger.getLogger(CertsFacade.class.getName()).log(Level.SEVERE, null, e);
    }
    return new UserCerts();
}

From source file:edu.sabanciuniv.sentilab.sare.controllers.entitymanagers.LexiconBuilderController.java

/**
 * Gets the unseen document with the next highest weight.
 * @param em the {@link EntityManager} to use.
 * @param builder the {@link LexiconBuilderDocumentStore} to use.
 * @return the {@link LexiconBuilderDocument} with the highest weight among unseen documents.
 *//* w ww  .ja  v  a  2 s  . co  m*/
public LexiconBuilderDocument getNextDocument(EntityManager em, LexiconBuilderDocumentStore builder) {
    Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em");
    Validate.notNull(builder, CannedMessages.NULL_ARGUMENT, "builder");

    TypedQuery<LexiconBuilderDocument> query = this.getDocumentsQuery(em, builder, false);
    query.setMaxResults(1);
    if (query.getResultList().size() != 1) {
        return null;
    }
    LexiconBuilderDocument document = query.getSingleResult();
    query = this.getDocumentsQuery(em, builder, null);
    document.setRank((long) query.getResultList().indexOf(document));
    return document;
}

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

@Override
public LegConfiguration getLegConfiguration(final String pModeKey) {
    final TypedQuery<LegConfiguration> query = this.entityManager
            .createNamedQuery("LegConfiguration.findByName", LegConfiguration.class);
    query.setParameter("NAME", this.getLegConfigurationNameFromPModeKey(pModeKey));
    return query.getSingleResult();
}

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

@Override
public Service getService(final String pModeKey) {
    final TypedQuery<Service> query = this.entityManager.createNamedQuery("Service.findByName", Service.class);
    query.setParameter("NAME", this.getServiceNameFromPModeKey(pModeKey)); //FIXME enable multiple ServiceTypes with the same name
    return query.getSingleResult();
}

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

@Test
public void testeVerificarQuantidadeAtividades() {
    Long quantidadeEsperada = 46L;
    TypedQuery<Long> query = super.entityManager.createQuery(
            "SELECT COUNT(a) FROM Atividade a WHERE EXISTS ( SELECT m FROM Monitoria m)", Long.class);
    Long quantidade = query.getSingleResult();
    assertEquals(quantidadeEsperada, quantidade);
}

From source file:org.openmeetings.app.data.flvrecord.FlvRecordingMetaDataDaoImpl.java

public FlvRecordingMetaData getFlvRecordingMetaDataById(Long flvRecordingMetaDataId) {
    try {//from   w  ww  .  j  a v  a  2 s  .  c o m

        String hql = "SELECT c FROM FlvRecordingMetaData c "
                + "WHERE c.flvRecordingMetaDataId = :flvRecordingMetaDataId";

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

        FlvRecordingMetaData flvRecordingMetaData = null;
        try {
            flvRecordingMetaData = query.getSingleResult();
        } catch (NoResultException ex) {
        }

        return flvRecordingMetaData;

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