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.flvrecord.FlvRecordingDaoImpl.java

public FlvRecording getFlvRecordingById(Long flvRecordingId) {
    try {//from   w ww .ja va  2  s.  c  om

        String hql = "SELECT c FROM FlvRecording c " + "WHERE c.flvRecordingId = :flvRecordingId";

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

        FlvRecording flvRecording = null;
        try {
            flvRecording = query.getSingleResult();
        } catch (NoResultException ex) {
        }
        return flvRecording;
    } catch (Exception ex2) {
        log.error("[getFlvRecordingById]: ", ex2);
    }
    return null;
}

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

public Fieldvalues getFieldvaluesById(Long fieldvalues_id) {
    String hql = "select f from Fieldvalues f WHERE f.fieldvalues_id = :fieldvalues_id ";
    TypedQuery<Fieldvalues> query = em.createQuery(hql, Fieldvalues.class);
    query.setParameter("fieldvalues_id", fieldvalues_id);
    Fieldvalues fv = null;//www  .  j  a v  a2  s. co  m
    try {
        fv = query.getSingleResult();
    } catch (NoResultException ex) {
    }
    return fv;
}

From source file:org.querybyexample.jpa.JpaUniqueUtil.java

private boolean existsInDatabaseOnAllObjects(Identifiable<?> entity, Map<String, Object> values) {
    if (entity == null || values == null || values.isEmpty()) {
        return false;
    }//from   ww w .j a  v  a 2  s  . co m
    String entityName = getEntityName(entity);
    String sqlQuery = "select count(c) from " + entityName + " c where";
    boolean first = true;
    for (Map.Entry<String, Object> property : values.entrySet()) {
        sqlQuery += !first ? " and " : " ";
        if (property.getValue() instanceof String) {
            sqlQuery += "upper(" + property.getKey() + ")=:" + property.getKey();
        } else {
            sqlQuery += property.getKey() + "=:" + property.getKey();
        }
        first = false;
    }
    if (entity.isIdSet()) {
        if (!first) {
            sqlQuery += " and";
        }
        sqlQuery += " id<>:id";
    }
    TypedQuery<Long> query = entityManager.createQuery(sqlQuery, Long.class);
    for (Map.Entry<String, Object> property : values.entrySet()) {
        String propertyName = property.getKey();
        Object value = property.getValue();
        if (value instanceof String) {
            value = ((String) value).toUpperCase(LocaleContextHolder.getLocale());
        }
        query.setParameter(propertyName, value);
    }
    if (entity.isIdSet()) {
        query.setParameter("id", entity.getId());
    }
    return query.getSingleResult() > 0;
}

From source file:org.terasoluna.tourreservation.domain.repository.tourinfo.TourInfoRepositoryImpl.java

protected long countSearchTourInfo(TourInfoSearchCriteria criteria) {
    String q = buildJpqlCount(criteria.getTourDays(), criteria.getBasePrice());
    TypedQuery<Long> query = entityManager.createQuery(q, Long.class);

    query.setParameter("adultCount", criteria.getAdultCount());
    query.setParameter("childCount", criteria.getChildCount());
    query.setParameter("depDay", criteria.getDepDate());
    query.setParameter("depCode", criteria.getDepCode());
    query.setParameter("arrCode", criteria.getArrCode());
    if (criteria.getTourDays() != 0) {
        query.setParameter("tourDays", criteria.getTourDays());
    }//from  w ww .  java 2 s  . com
    if (criteria.getBasePrice() != 0) {
        query.setParameter("basePrice", criteria.getBasePrice());
    }
    Long tourCount = query.getSingleResult();
    if (tourCount == null) {
        tourCount = 0L;
    }
    return tourCount;
}

From source file:com.dbs.sdwt.jpa.JpaUniqueUtil.java

private boolean existsInDatabaseOnAllObjects(Identifiable<?> entity, Map<String, Object> values) {
    if (entity == null || values == null || values.isEmpty()) {
        return false;
    }/* w ww . jav  a2 s  . c  om*/
    String entityName = jpaUtil.getEntityName(entity);
    String sqlQuery = "select count(c) from " + entityName + " c where";
    boolean first = true;
    for (Map.Entry<String, Object> property : values.entrySet()) {
        sqlQuery += !first ? " and " : " ";
        if (property.getValue() instanceof String) {
            sqlQuery += "upper(" + property.getKey() + ")=:" + property.getKey();
        } else {
            sqlQuery += property.getKey() + "=:" + property.getKey();
        }
        first = false;
    }
    if (entity.isIdSet()) {
        if (!first) {
            sqlQuery += " and";
        }
        sqlQuery += " id<>:id";
    }
    TypedQuery<Long> query = entityManager.createQuery(sqlQuery, Long.class);
    for (Map.Entry<String, Object> property : values.entrySet()) {
        String propertyName = property.getKey();
        Object value = property.getValue();
        if (value instanceof String) {
            value = ((String) value).toUpperCase(LocaleContextHolder.getLocale());
        }
        query.setParameter(propertyName, value);
    }
    if (entity.isIdSet()) {
        query.setParameter("id", entity.getId());
    }
    return query.getSingleResult() > 0;
}

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

/**
 * Gets the by logical id.//from w w  w .  j  a  v a  2s .c  o  m
 * 
 * @param idEntity
 *            the id entity
 * @return the by logical id
 * @since 2/09/2014, 09:31:42 PM
 */
@Override
public WorkRequestLocations getByLogicalId(Serializable idEntity) {
    TypedQuery<WorkRequestLocations> query = entityManager.createNamedQuery(
            "WorkRequestLocations." + NormalizedEntity.NAMED_QUERY_ID, WorkRequestLocations.class);
    query.setParameter("idService", ((WorkRequestsPK) idEntity).getIdWorkRequests());
    query.setParameter("categoryPrefix", ((WorkRequestsPK) idEntity).getCategoryId());
    return query.getSingleResult();
}

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

/**
 * {@inheritDoc}//from www  .j a v a  2  s  .  c  o m
 */
@Override
public <T> T getScalarResultOfType(Class<T> resultClass, String namedQuery, List<Object> posParams) {
    EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory());
    try {
        TypedQuery<T> query = em.createNamedQuery(namedQuery, resultClass);
        JpaUtil.bindQueryParams(query, posParams);
        return query.getSingleResult();
    } catch (Exception ex) { // NOSONAR
        throw new JpaException(ex);
    } finally {
        JpaUtil.closeEntityManager(em);
    }
}

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

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

    SyncopeUser result = null;/* ww w  .  j av a 2  s . co m*/
    try {
        return query.getSingleResult();
    } catch (NoResultException e) {
    }

    return result;
}

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

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

    SyncopeUser result = null;/*from   w  w w .j a  va  2  s.  c o  m*/
    try {
        return query.getSingleResult();
    } catch (NoResultException e) {
    }

    return result;
}

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

public int updatePrivateMessagesReadStatus(List<Long> privateMessageIds, Boolean isRead) {
    try {/*  w w  w.ja  v a2  s. c  o m*/

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

        Query query = em.createQuery(hql);
        query.setParameter("isRead", isRead);
        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;
}