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:nl.npcf.eav.store.EntityAttributeValueStore.java

@Transactional(rollbackFor = EAVValidationException.class)
public Entity setValue(String entityKey, Path path, String valueString, Principal createdBy)
        throws EAVValidationException {
    EAVAttribute attribute = (EAVAttribute) getEAVSchema().getAttribute(path, true);
    if (attribute.isAggregate()) {
        throw EAVValidationException.cannotBeSet(attribute);
    }/*from  ww  w .j  av a 2s .c  o m*/
    Query query = entityManager.createQuery("select entity from EAVEntity entity where entity.key = :key");
    query.setParameter("key", entityKey);
    EAVEntity entity = (EAVEntity) query.getSingleResult();
    entity.setSchema(getSchema());
    Object value = attribute.stringToValue(valueString); //validate first
    Iterator<EAVValue> valueIterator = entity.getValues().iterator();
    while (valueIterator.hasNext()) { // clear any existing value
        EAVValue existing = valueIterator.next();
        if (existing.getPath().equals(path)) {
            valueIterator.remove();
            entityManager.remove(existing);
        }
    }
    EAVValue eavValue = attribute.instantiate(entity, (EAVPath) path, value, createdBy);
    entity.getValues().add(eavValue);
    eavValue.getPath();
    entity.validate();
    entity.sortValues();
    return entity;
}

From source file:DAO.PersonnesDAOImpl.java

@Transactional(readOnly = true)
@Override/*  w w  w  .j  ava 2 s  . c o  m*/
public List<PersonnesEntity> findFilous(int id) {
    Query q = em.createQuery("SELECT pl FROM PersonnesEntity P JOIN P.listFilous pl " + "WHERE P.id = ?");
    q.setParameter(1, id);
    return ((PersonnesEntity) q.getSingleResult()).getListFilous();
}

From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java

/**
 * count all entity/*  ww w .j  av  a2  s .  co m*/
 *
 * @return
 */
public int count() {
    EntityManager em = getEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery cq = cb.createQuery();
    Root<T> rt = cq.from(entityClass);
    cq.select(cb.count(rt));
    Query q = em.createQuery(cq);
    Object result = q.getSingleResult();
    return result == null ? 0 : ((Long) result).intValue();
}

From source file:cn.guoyukun.spring.jpa.repository.RepositoryHelper.java

/**
 * <p>?ql?paramsql<br/>/*from  w w w  . j a  v a 2 s.c o m*/
 * ?cn.guoyukun.spring.jpa.repository.UserRepository2ImplIT#testCountAll()
 *
 * @param ql
 * @param params
 * @return
 */
public long count(final String ql, final Object... params) {

    Query query = entityManager.createQuery(ql);
    applyEnableQueryCache(query);
    setParameters(query, params);

    return (Long) query.getSingleResult();
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.test.CommandITCase.java

/**
 * Count articles./* w ww .  j av a 2s.  com*/
 *
 * @return the long
 * @throws Exception the exception
 */
private long countArticles() throws Exception {
    EntityManager em = emf.createEntityManager();
    Query query = em.createQuery("SELECT COUNT(p.title) FROM article p");
    Long countResult = (Long) query.getSingleResult();

    em.close();

    return countResult;
}

From source file:nl.npcf.eav.store.EntityAttributeValueStore.java

@Transactional(rollbackFor = EAVValidationException.class)
public Entity setValues(String entityKey, Map<Path, String> valueMap, Principal createdBy)
        throws EAVValidationException {
    Query query = entityManager.createQuery("select entity from EAVEntity entity where entity.key = :key");
    query.setParameter("key", entityKey);
    EAVEntity entity = (EAVEntity) query.getSingleResult();
    entity.setSchema(getSchema());//from w  w  w  . ja  v a  2 s  .  co m
    for (Map.Entry<Path, String> entry : valueMap.entrySet()) {
        EAVAttribute attribute = (EAVAttribute) getEAVSchema().getAttribute(entry.getKey(), true);
        if (attribute.isAggregate()) {
            throw EAVValidationException.cannotBeSet(attribute);
        }
        Iterator<EAVValue> valueIterator = entity.getValues().iterator();
        while (valueIterator.hasNext()) { // clear any existing value
            EAVValue existing = valueIterator.next();
            if (existing.getPath().equals(entry.getKey())) {
                valueIterator.remove();
                entityManager.remove(existing);
            }
        }
        if (!entry.getValue().isEmpty()) {
            Object value = attribute.stringToValue(entry.getValue()); //validate first
            EAVValue eavValue = attribute.instantiate(entity, (EAVPath) entry.getKey(), value, createdBy);
            entity.getValues().add(eavValue);
            eavValue.getPath();
        }
    }
    entity.validate();
    entity.sortValues();
    return entity;
}

From source file:com.taobao.ad.easyschedule.base.JPABaseDAO.java

public Object executeSingleIsNotEmpty(String queryString, final Map<String, Object> map, String orderBy) {

    final List<String> keyList = getKeyList(map);
    final String queryStringCallBack = getQueryString(queryString, keyList, orderBy);
    return this.getJpaTemplate().execute(new JpaCallback() {

        @Override// ww w. j  a  v a  2 s.  c o m
        public Object doInJpa(EntityManager em) throws PersistenceException {
            Query query = em.createQuery(queryStringCallBack);
            for (int i = 0; i < keyList.size(); i++) {
                query.setParameter((i + 1), map.get(keyList.get(i)));
            }
            query.setMaxResults(1);
            try {
                return query.getSingleResult();
            } catch (Exception e) {
                return null;
            }
        }
    });

}

From source file:DAO.StatutsDAOImpl.java

@Transactional(readOnly = true)
@Override/*from   w w w  . j av a 2s  . c  om*/
public StatutsEntity find(int id) {
    Query q = em.createQuery("SELECT s FROM StatutsEntity s where s.id = ?");
    q.setParameter(1, id);
    try {
        return (StatutsEntity) q.getSingleResult();
    } catch (NoResultException e) {
        return null;
    }
}

From source file:com.healthcit.cacure.dao.BaseQuestionDao.java

public boolean isQuestionAlreadyExistsInForm(Long formId, Long id) {
    //      String jpql = "select count(*) from BaseQuestion q , FormElement e where q.parent.id = e.id and e.form.id = :formId and (q.uuid = :uuid or q.linkId = :uuid) and linkId is not null";
    /* Only questions that are imported wil be links */
    String sql = "select count(*) from form_element e where e.form_id = :formId and e.link_id = :uuid";
    //Query query = em.createQuery(jpql);
    Query query = em.createNativeQuery(sql);
    query.setParameter("formId", formId);
    query.setParameter("uuid", id);
    int count = Integer.valueOf(query.getSingleResult().toString());
    return count > 0;
}

From source file:org.apache.cxf.fediz.service.idp.service.jpa.ApplicationDAOJPAImpl.java

@Override
public void deleteApplication(String realm) {
    Query query = null;
    query = em.createQuery("select a from Application a where a.realm=:realm");
    query.setParameter("realm", realm);

    //@SuppressWarnings("rawtypes")
    Object applObj = query.getSingleResult();
    em.remove(applObj);//www  .j a va 2s  .  co  m

    LOG.debug("Application '{}' deleted", realm);

}