Example usage for javax.persistence Query getSingleResult

List of usage examples for javax.persistence Query getSingleResult

Introduction

In this page you can find the example usage for javax.persistence Query getSingleResult.

Prototype

Object getSingleResult();

Source Link

Document

Execute a SELECT query that returns a single untyped result.

Usage

From source file:fr.amapj.service.services.meslivraisons.MesLivraisonsService.java

/**
 * Indique si il y a au moins une livraison cette semaine
 * @param dto//from  w ww. j av a2 s  .c o  m
 * @return
 */
private boolean hasLivraison(EntityManager em, Date dateDebut, Date dateFin) {
    // On extrait toutes les livraisons sur l'intervalle 
    Query q = em.createQuery("select count(mcd.dateLiv) from ModeleContratDate mcd WHERE "
            + "mcd.dateLiv>=:deb AND " + "mcd.dateLiv<=:fin ");
    q.setParameter("deb", dateDebut, TemporalType.DATE);
    q.setParameter("fin", dateFin, TemporalType.DATE);

    return LongUtils.toInt(q.getSingleResult()) > 0;
}

From source file:org.venice.piazza.common.hibernate.dao.job.JobDaoImpl.java

public Page<JobEntity> getJobListByStatus(String status, Pagination pagination) {
    // Query//  w ww  .j  ava  2s  .  c  om
    String queryString = String.format(STATUS_JOB_QUERY, Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, JobEntity.class);
    query.setParameter(1, status);
    query.setParameter(2, pagination.getSortBy());
    query.setParameter(3, pagination.getPerPage());
    query.setParameter(4, pagination.getPage() * pagination.getPerPage());
    List<JobEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(STATUS_JOB_QUERY_COUNT);
    query.setParameter(1, status);
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<JobEntity>(results, null, count);
}

From source file:com.expressui.core.dao.GenericDao.java

/**
 * Gets a count of all entities of given type.
 *
 * @param entityType the type of entity//from   ww  w  . j a v  a 2  s.co m
 * @param <T>        type of entity
 * @return count of all records in the database
 */
public <T> Long countAll(Class<? extends T> entityType) {
    Query query = getEntityManager().createQuery("SELECT COUNT(e) from " + entityType.getSimpleName() + " e");

    return (Long) query.getSingleResult();
}

From source file:com.jada.admin.customAttribute.CustomAttributeGroupMaintAction.java

public ActionForward removeCustomAttributeDetails(ActionMapping mapping, ActionForm actionForm,
        HttpServletRequest request, HttpServletResponse response) throws Throwable {
    EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
    CustomAttributeGroupMaintActionForm form = (CustomAttributeGroupMaintActionForm) actionForm;
    AdminBean adminBean = getAdminBean(request);
    Site site = adminBean.getSite();/*from  ww w  .j ava 2 s. com*/

    JSONEscapeObject jsonResult = new JSONEscapeObject();
    jsonResult.put("status", Constants.WEBSERVICE_STATUS_SUCCESS);

    Long customAttribGroupId = Format.getLong(form.getCustomAttribGroupId());
    CustomAttributeGroup customAttributeGroup = CustomAttributeGroupDAO.load(site.getSiteId(),
            customAttribGroupId);
    String customAttribDetailIds[] = form.getCustomAttribDetailIds();
    if (customAttribDetailIds != null) {
        for (int i = 0; i < customAttribDetailIds.length; i++) {
            CustomAttributeDetail customAttributeDetail = (CustomAttributeDetail) em
                    .find(CustomAttributeDetail.class, Format.getLong(customAttribDetailIds[i]));
            if (customAttributeDetail.getCustomAttribute()
                    .getCustomAttribTypeCode() == Constants.CUSTOM_ATTRIBUTE_TYPE_SKU_MAKEUP) {
                String sql = "select  count(*) " + "from     ItemAttributeDetail itemAttributeDetail "
                        + "where     itemAttributeDetail.customAttributeDetail = :customAttributeDetail "
                        + "and     itemAttributeDetail.item.itemTypeCd = :itemTypeCd";
                Query query = em.createQuery(sql);
                query.setParameter("customAttributeDetail", customAttributeDetail);
                query.setParameter("itemTypeCd", Constants.ITEM_TYPE_SKU);
                Long count = (Long) query.getSingleResult();
                if (count.intValue() > 0) {
                    jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
                    jsonResult.put("reason", Constants.WEBSERVICE_REASON_INUSE);
                    streamWebService(response, jsonResult.toHtmlString());
                    return null;
                }
            }

            String sql = "delete " + "from   ItemAttributeDetail itemAttributeDetail "
                    + "where  itemAttributeDetail.customAttributeDetail = :customAttributeDetail ";
            Query query = em.createQuery(sql);
            query.setParameter("customAttributeDetail", customAttributeDetail);
            query.executeUpdate();

            customAttributeGroup.getCustomAttributeDetails().remove(customAttributeDetail);
            em.remove(customAttributeDetail);
        }
    }
    streamWebService(response, jsonResult.toHtmlString());
    return null;
}

From source file:org.venice.piazza.common.hibernate.dao.job.JobDaoImpl.java

public Page<JobEntity> getJobListByUser(String userName, Pagination pagination) {
    // Query//from   w  w  w  .  ja  va2 s  . c  o  m
    String queryString = String.format(USERNAME_JOB_QUERY, Direction.fromString(pagination.getOrder()));
    Query query = entityManager.createNativeQuery(queryString, JobEntity.class);
    query.setParameter(1, userName);
    query.setParameter(2, pagination.getSortBy());
    query.setParameter(3, pagination.getPerPage());
    query.setParameter(4, pagination.getPage() * pagination.getPerPage());
    List<JobEntity> results = query.getResultList();
    // Count
    query = entityManager.createNativeQuery(USERNAME_JOB_QUERY_COUNT);
    query.setParameter(1, userName);
    long count = ((BigInteger) query.getSingleResult()).longValue();
    return new PageImpl<JobEntity>(results, null, count);
}

From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java

@Override
public Long countDevices(String deviceType) {
    Query query = this.getEntityManager().createNamedQuery("Device.countDevicesForType");
    query.setParameter("deviceType", deviceType);
    return (Long) query.getSingleResult();
}

From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java

public Long countDevicesWithUsername(String username) {
    Query query = this.getEntityManager().createNamedQuery("Device.countDevicesWithUsername");
    query.setParameter("username", username);
    return (Long) query.getSingleResult();
}

From source file:com.sun.socialsite.business.impl.JPARelationshipManagerImpl.java

public void removeRelationship(Profile from, Profile to) throws SocialSiteException {

    Query query1 = strategy.getNamedQuery("Relationship.getByProfileFromAndProfileTo");
    query1.setParameter(1, from);//w  ww. j a va 2  s . c o  m
    query1.setParameter(2, to);
    Relationship rel1 = (Relationship) query1.getSingleResult();
    strategy.remove(rel1);
}

From source file:org.kuali.mobility.push.dao.DeviceDaoImpl.java

public boolean doesDeviceHaveUsername(String deviceid) {
    Query query = getEntityManager().createNamedQuery("Device.hasUsernameForDeviceId");
    query.setParameter("deviceId", deviceid);
    Long matches = (Long) query.getSingleResult();
    return (matches.intValue() != 0);
}

From source file:es.ucm.fdi.dalgs.subject.repository.SubjectRepository.java

public Subject getSubject(Long id, Long id_topic, Long id_module, Long id_degree) {
    Topic topic = em.getReference(Topic.class, id_topic);
    Module module = em.getReference(Module.class, id_module);
    Degree degree = em.getReference(Degree.class, id_degree);

    Query query = em.createQuery(
            "Select s from Subject s where s.id=?1 and s.topic=?2 and s.topic.module=?3 and s.topic.module.degree=?4");
    query.setParameter(1, id);/*from   w w w.j  a va 2s.c om*/
    query.setParameter(2, topic);
    query.setParameter(3, module);
    query.setParameter(4, degree);

    if (query.getResultList().isEmpty())
        return null;
    else
        return (Subject) query.getSingleResult();

}