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.basic.Configurationmanagement.java

public Configuration getConfByConfigurationId(long user_level, long configuration_id) {
    try {//from w ww . j  a v a  2 s  . c  om
        log.debug("getConfByConfigurationId1: user_level " + user_level);
        if (authLevelManagement.checkAdminLevel(user_level)) {
            Configuration configuration = null;
            TypedQuery<Configuration> query = em.createQuery(
                    "select c from Configuration as c where c.configuration_id = :configuration_id",
                    Configuration.class);
            query.setParameter("configuration_id", configuration_id);
            query.setMaxResults(1);
            try {
                configuration = query.getSingleResult();
            } catch (NoResultException e) {
            }
            log.debug("getConfByConfigurationId4: " + configuration);

            if (configuration != null && configuration.getUser_id() != null) {
                configuration.setUsers(usersDao.getUser(configuration.getUser_id()));
            }
            return configuration;
        } else {
            log.error("[getConfByConfigurationId] Permission denied " + user_level);
        }
    } catch (Exception ex2) {
        log.error("[getConfByConfigurationId]: ", ex2);
    }
    return null;
}

From source file:org.jdal.dao.jpa.JpaDao.java

/**
 * Gets a NamedQuery from page, setup order, params and page result count.
 * @param page request page// www  . ja v a 2  s .  c o m
 * @return a TypedQuery from a NamedQuery
 */
@SuppressWarnings("unchecked")
protected <K> TypedQuery<K> getNamedQuery(Page<K> page) {
    Filter filter = null;
    TypedQuery<K> query = null;

    if (page.getFilter() instanceof Filter) {
        filter = (Filter) page.getFilter();
        String queryString = getQueryString(filter.getFilterName());
        if (queryString != null) {
            String countQueryString = JpaUtils.createCountQueryString(queryString);
            TypedQuery<Long> countQuery = em.createQuery(countQueryString, Long.class);
            applyFilter(countQuery, filter);
            page.setCount(countQuery.getSingleResult().intValue());
            // add Order
            if (page.getSortName() != null)
                queryString = JpaUtils.addOrder(queryString, page.getSortName(),
                        page.getOrder() == Page.Order.ASC);

            query = (TypedQuery<K>) em.createQuery(queryString);
            applyFilter(query, filter);
        }
    }

    return query;
}

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

/**
 * Gets the by logical id.//from  ww w .  ja  v a2s.  co  m
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:50 PM
 */
@Override
public ServiceCategories getByLogicalId(Serializable idEntity) {
    TypedQuery<ServiceCategories> query = entityManager.createNamedQuery(
            ServiceCategories.class.getSimpleName() + ".findByCategoryPrefix", ServiceCategories.class);
    query.setParameter("categoryPrefix", (String) idEntity);
    return query.getSingleResult();
}

From source file:ec.edu.chyc.manejopersonal.controller.PersonaJpaController.java

public Persona findPersona(Long id, int incluir) {
    //public Persona findPersona(Long id, boolean incluirArticulos, boolean incluirContratos, boolean incluirTitulos, boolean incluirTesis, boolean incluirPasantias, boolean incluirFirmas) {
    EntityManager em = null;/*ww w . j av a  2  s .co m*/
    try {
        em = getEntityManager();
        //String incluir = "";
        ArrayList<String> listaFetchs = new ArrayList<>();
        /*int totalFetchs = 0;
        if (incluirContratos) {
        listaFetchs.add(" left join fetch p.contratosCollection ");
        totalFetchs++;
        }            
        if (incluirArticulos) {
        listaFetchs.add(" left join fetch p.personaArticulosCollection ");
        totalFetchs++;
        }            
        if (incluirTitulos) {
        listaFetchs.add(" left join fetch p.personaTitulosCollection ");
        totalFetchs++;
        }
        if (incluirTesis) {
        listaFetchs.add(" left join fetch p.tesisCollection ");
        totalFetchs++;                
        }
        if (incluirPasantias) {
        listaFetchs.add(" left join fetch p.tesisCollection ");
        totalFetchs++;                
        }
        if (incluirFirmas) {
        listaFetchs.add(" left join fetch p.personaFirmasCollection ");
        totalFetchs++;                
        }            
        if (listaFetchs.size() > 1) {
        incluir = listaFetchs.get(0);
        }*/

        TypedQuery<Persona> q = em.createQuery("select p from Persona p where p.id=:id", Persona.class);
        q.setParameter("id", id);
        Persona p = q.getSingleResult();

        if ((incluir & Incluir.INC_CONTRATOS.value()) != 0) {
            for (Contrato contrato : p.getContratosCollection()) {
                Hibernate.initialize(contrato.getProyectosCollection());
            }
        }
        if ((incluir & Incluir.INC_ARTICULOS.value()) != 0) {
            //if (incluirArticulos) {
            //Hibernate.initialize(p.getPersonaArticulosCollection());
            //Hibernate.initialize(p.getPersonaArticulosCollection());
            p.getListaPersonaArticulos().clear();
            for (PersonaFirma perFirma : p.getPersonaFirmasCollection()) {
                for (PersonaArticulo perArticulo : perFirma.getPersonasArticulosCollection()) {
                    p.getListaPersonaArticulos().add(perArticulo);
                }
            }
        }
        if ((incluir & Incluir.INC_TITULOS.value()) != 0) {
            //if (incluirTitulos) {
            Hibernate.initialize(p.getPersonaTitulosCollection());
        }
        if ((incluir & Incluir.INC_TESIS.value()) != 0) {
            //if (incluirTesis) {
            Hibernate.initialize(p.getTesisCollection());
        }
        if ((incluir & Incluir.INC_PASANTIAS.value()) != 0) {
            //if (incluirPasantias) {
            Hibernate.initialize(p.getPasantiasCollection());
        }
        if ((incluir & Incluir.INC_FIRMAS.value()) != 0) {
            //if (incluirFirmas) {
            Hibernate.initialize(p.getPersonaFirmasCollection());
        }
        return p;
    } finally {
        if (em != null) {
            em.close();
        }
    }
}

From source file:com.uni.dao.etc.UniJpaRepository.java

public boolean exists(ID id) {

    Assert.notNull(id, "The given id must not be null!");

    if (entityInformation.getIdAttribute() != null) {

        String placeholder = provider.getCountQueryPlaceholder();
        String entityName = entityInformation.getEntityName();
        String idAttributeName = entityInformation.getIdAttribute().getName();
        String existsQuery = String.format(EXISTS_QUERY_STRING, placeholder, entityName, idAttributeName);

        TypedQuery<Long> query = em.createQuery(existsQuery, Long.class);
        query.setParameter("id", id);

        return query.getSingleResult() == 1;
    } else {//from  ww w. java2 s .c  o  m
        return findOne(id) != null;
    }
}

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

/**
 * Gets the by logical id./*from   w  w  w . java 2 s  .  c  o  m*/
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:41 PM
 */
@Override
public BusinessLines getByLogicalId(Serializable idEntity) {
    if (idEntity instanceof String) {
        TypedQuery<BusinessLines> query = entityManager.createNamedQuery("BusinessLines.findByLobPrefix",
                BusinessLines.class);
        query.setParameter("lobPrefix", (String) idEntity);
        return query.getSingleResult();
    } else {
        TypedQuery<BusinessLines> query = entityManager.createNamedQuery(
                BusinessLines.class.getSimpleName() + "." + NormalizedEntity.NAMED_QUERY_ID,
                BusinessLines.class);
        query.setParameter("idbusinessLine", (Integer) idEntity);
        return query.getSingleResult();
    }
}

From source file:com.uni.dao.etc.UniJpaRepository.java

public T findOneBy(String fieldName, String fieldValue) {
    String stringQuery = "SELECT u FROM " + getDomainClass().getCanonicalName() + " u " + "WHERE u." + fieldName
            + " = ?1";
    System.out.println(stringQuery);
    TypedQuery<T> query = em.createQuery(stringQuery, getDomainClass());
    query.setParameter(1, fieldValue);/* w  w  w. j a v a  2  s .c  o  m*/
    try {
        return query.getSingleResult();
    } catch (NoResultException e) {
        return null;
    } catch (NonUniqueResultException e) {
        return null;
    }
}

From source file:bzzAgent.BiteSizedBzzDaoJpa.java

@Override
public BiteSizedActivityEntity getByIdent(String activityIdent) {

    final TypedQuery<BiteSizedActivityEntity> result = em
            .createQuery(" select bsb from BiteSizedActivityEntity bsb" +
            //" join fetch bsb.bzzActivityEntity ba " +
                    " left join fetch bsb.incentives incentive " + " join fetch bsb.campaignEntity c "
                    + " join fetch c.campaignPermissionsEntity cp "
                    + " left join fetch c.campaignProfiles profiles " + " where bsb.activityIdent=:ident",
                    BiteSizedActivityEntity.class)
            .setParameter("ident", activityIdent);
    return result.getSingleResult();
}

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

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

    return query.getSingleResult();
}

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

protected String findLegName(final String agreementRef, final String senderParty, final String receiverParty,
        final String service, final String action) throws EbMS3Exception {
    final Query candidatesQuery = this.entityManager
            .createNamedQuery("LegConfiguration.findForPartiesAndAgreements");
    candidatesQuery.setParameter("AGREEMENT", agreementRef);
    candidatesQuery.setParameter("SENDER_PARTY", senderParty);
    candidatesQuery.setParameter("RECEIVER_PARTY", receiverParty);
    final List<LegConfiguration> candidates = candidatesQuery.getResultList();
    if (candidates == null || candidates.isEmpty()) {
        throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0001, "No Candidates for Legs found", null,
                null, null);/*from   ww w. j  a v  a  2 s  .  c o  m*/
    }
    final TypedQuery<String> query = this.entityManager.createNamedQuery("LegConfiguration.findForPMode",
            String.class);
    query.setParameter("SERVICE", service);
    query.setParameter("ACTION", action);
    final Collection<String> candidateIds = new ArrayList();
    for (final LegConfiguration candidate : candidates) {
        candidateIds.add(candidate.getName());
    }
    query.setParameter("CANDIDATES", candidateIds);
    try {
        return query.getSingleResult();
    } catch (final NoResultException e) {
        PModeDao.LOG.info("", e);
    }
    throw new EbMS3Exception(EbMS3Exception.EbMS3ErrorCode.EBMS_0001, "No matching leg found", null, null,
            null);
}