List of usage examples for javax.persistence Query getSingleResult
Object getSingleResult();
From source file:org.venice.piazza.common.hibernate.dao.dataresource.DataResourceDaoImpl.java
public Page<DataResourceEntity> getDataResourceListByUser(String userName, Pagination pagination) { // Query/*w ww . ja va2 s .c o m*/ String queryString = String.format(USERNAME_DATARESOURCE_QUERY, Direction.fromString(pagination.getOrder())); Query query = entityManager.createNativeQuery(queryString, DataResourceEntity.class); query.setParameter(1, String.format(WILDCARD_STRING_QUERY, userName)); query.setParameter(2, pagination.getSortBy()); query.setParameter(3, pagination.getPerPage()); query.setParameter(4, pagination.getPage() * pagination.getPerPage()); List<DataResourceEntity> results = query.getResultList(); // Count query = entityManager.createNativeQuery(USERNAME_DATARESOURCE_QUERY_COUNT); query.setParameter(1, String.format(WILDCARD_STRING_QUERY, userName)); long count = ((BigInteger) query.getSingleResult()).longValue(); return new PageImpl<DataResourceEntity>(results, null, count); }
From source file:com.hiperium.dao.common.generic.GenericDAO.java
/** * /*from www . j av a 2s . c om*/ * @param entity * @param bypassCache * @return */ protected Long count(T entity, boolean bypassCache) { this.logger.debug("count - START"); CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class); Root<T> root = criteriaQuery.from(this.entityClass); criteriaQuery.select(criteriaBuilder.count(root)); // Create JPA Query Query query = this.entityManager.createQuery(criteriaQuery); if (bypassCache) { this.setBypassCahe(query); } // get the query results Long result = (Long) query.getSingleResult(); this.logger.debug("count - END: " + result); return result; }
From source file:org.opentides.dao.impl.BaseEntityDaoJpaImpl.java
/** * {@inheritDoc}//from w ww . ja v a2 s. c om */ @Override public final long countByExample(final T example, final boolean exactMatch) { String whereClause = CrudUtil.buildJpaQueryString(example, exactMatch); String filterClause = this.buildSecurityFilterClause(example); String append = appendClauseToExample(example, exactMatch); whereClause = doSQLAppend(whereClause, append); whereClause = doSQLAppend(whereClause, filterClause); if (_log.isDebugEnabled()) _log.debug("Count QBE >> " + whereClause); String sql = "select count(obj) from " + getEntityBeanType().getName() + " obj " + whereClause; Query query = getEntityManager().createQuery(sql); setQueryParameters(query, sql, example); setHints(example, query); return (Long) query.getSingleResult(); }
From source file:com.espirit.moddev.examples.uxbridge.newswidget.jpa.ArticleHandler.java
/** * Add or update a news article in the db * * @param entity The NewsItem/*from ww w. j a v a2s . c o m*/ */ public void add(UXBEntity entity) throws Exception { Article art = buildArticle(entity); EntityManager em = null; EntityTransaction tx = null; try { em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); Query query = em.createQuery(new StringBuilder() .append("SELECT x FROM article x WHERE x.aid = :fsid AND x.language=:language").toString()); query.setParameter("fsid", art.getAid()); query.setParameter("language", art.getLanguage()); /* * If the item exists in the db, we update the content and the title of the existing item */ if (query.getResultList().size() > 0) { Article db = (Article) query.getSingleResult(); db.setContent(art.getContent()); db.setTitle(art.getTitle()); db.setUrl(art.getUrl()); db.setCreated(art.getCreated()); db.setAid(art.getAid()); db.setLanguage(art.getLanguage()); db.setVersion(art.getVersion()); db.setLastmodified(art.getLastmodified()); art = db; } // save to db em.persist(art); em.flush(); tx.commit(); } catch (Exception e) { if (tx != null) { tx.setRollbackOnly(); throw e; } logger.error("Failure while writing to the database", e); } finally { if (tx != null && tx.isActive()) { if (tx.getRollbackOnly()) { tx.rollback(); } } if (em != null) { em.close(); } } }
From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java
@Override public boolean hasContent(Audit audit) { Query query = entityManager.createQuery("SELECT count(s.id) FROM " + SSPImpl.class.getName() + " s" + " JOIN s.audit as a" + " WHERE a = :audit" + " AND s.source != null " + " AND s.source != '' )"); query = query.setParameter(AUDIT_KEY, audit); return (Long) query.getSingleResult() > 0; }
From source file:edu.harvard.iq.dvn.core.study.StudyFileServiceBean.java
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) public Long getCountFilesByStudyVersion(Long svId) { String queryString = "SELECT count(f.*) FROM FileMetadata f WHERE f.studyVersion_id = " + svId + " "; Query query = em.createNativeQuery(queryString); Long retVal = (Long) query.getSingleResult(); return retVal; }
From source file:org.asqatasun.entity.dao.audit.ContentDAOImpl.java
@Override public Content find(WebResource page, String uri) { Query query = entityManager.createQuery("SELECT c FROM " + getEntityClass().getName() + " c" + " WHERE c.page = :page " + " AND c.uri = :uri"); query.setParameter("page", page); query.setParameter("uri", uri); try {//from w ww. j a v a 2s . co m return (Content) query.getSingleResult(); } catch (NoResultException nre) { return null; } catch (NonUniqueResultException nure) { List<Content> queryResult = query.getResultList(); for (Content content : queryResult) { if (StringUtils.equals(content.getURI(), uri)) { return content; } } return null; } }
From source file:com.healthcit.cacure.dao.FormElementDao.java
/** * @param q//ww w . ja va2 s.c om * String * @return List<Question> */ @SuppressWarnings("unchecked") public List<FormElement> getFormElementsByText(String q) { List<FormElement> elements = new ArrayList<FormElement>(); // List<Object> result = Collections.emptyList(); List<BigInteger> result = Collections.emptyList(); if (StringUtils.isNotEmpty(q)) { String query = q.replaceAll("'", "''"); Query nativeQuery = em.createNativeQuery("select q.id" + " from form_element q, form f, plainto_tsquery('ts_config', '" + query + "') query" + " where q.form_id = f.id AND f.form_type = 'questionLibraryForm' AND q.ts_data @@ query" + " order by ts_rank_cd(q.ts_data, query) desc"); result = nativeQuery.getResultList(); Query selectElement = em.createQuery("from FormElement fe where id = :id"); for (BigInteger id : result) { selectElement.setParameter("id", id.longValue()); elements.add((FormElement) selectElement.getSingleResult()); } } return elements; }
From source file:mil.navy.med.dzreg.dao.RegistriesManagerDAO.java
/** * Returns the registry profile of a person. * @param personId unique identifier of person * @return PersonRegistryProfileType//from w w w.java 2 s . c o m */ public PersonRegistryProfileType getRegistryProfile(Long personId) { EntityManager em = null; PersonRegistryProfileType profile = null; PersistentServiceFactory psf = null; if (personId != null) { try { psf = PersistentServiceFactory.getInstance(REGISTRY_MANAGER_PU); em = psf.getEntityManager(); Query query = em.createNamedQuery("DzPatients.findByPatid"); query.setParameter("patid", personId); DzPatients result = (DzPatients) query.getSingleResult(); if (result != null) { em.refresh(result); //log.debug(result.toString()); try { profile = map(result); } catch (Exception e) { e.printStackTrace(); } } } catch (javax.persistence.NoResultException nr) { log.error("No data found for personId=" + personId); } finally { em.close(); } } else { log.error("personId can not be null or empty"); } return profile; }
From source file:org.venice.piazza.common.hibernate.dao.dataresource.DataResourceDaoImpl.java
public Page<DataResourceEntity> getDataResourceListByCreatedJobId(String createdByJobId, Pagination pagination) {/* w ww. j a v a 2 s .co m*/ // Query String queryString = String.format(CREATED_BY_JOB_ID_DATARESOURCE_QUERY, Direction.fromString(pagination.getOrder())); Query query = entityManager.createNativeQuery(queryString, DataResourceEntity.class); query.setParameter(1, String.format(WILDCARD_STRING_QUERY, createdByJobId)); query.setParameter(2, pagination.getSortBy()); query.setParameter(3, pagination.getPerPage()); query.setParameter(4, pagination.getPage() * pagination.getPerPage()); List<DataResourceEntity> results = query.getResultList(); // Count query = entityManager.createNativeQuery(CREATED_BY_JOB_ID_DATARESOURCE_QUERY_COUNT); query.setParameter(1, String.format(WILDCARD_STRING_QUERY, createdByJobId)); long count = ((BigInteger) query.getSingleResult()).longValue(); return new PageImpl<DataResourceEntity>(results, null, count); }