List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:org.fao.geonet.repository.HarvesterSettingRepositoryOverridesImpl.java
@Override public List<Integer> findAllChildIds(int parentid) { CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> query = criteriaBuilder.createQuery(Integer.class); Root<HarvesterSetting> root = query.from(HarvesterSetting.class); query.where(criteriaBuilder.equal(root.get(HarvesterSetting_.parent), parentid)); query.select(root.get(HarvesterSetting_.id)); return _entityManager.createQuery(query).getResultList(); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * count all entity/* ww w. j av a 2 s . com*/ * * @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:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java
@Override public List<Translation> readAllTranslationEntries(TranslatedEntity entityType, ResultType stage) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class); Root<TranslationImpl> root = criteria.from(TranslationImpl.class); criteria.select(root); List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(builder.equal(root.get("entityType"), entityType.getFriendlyType())); try {//w w w . j a va 2 s . c o m if (extensionManager != null) { extensionManager.getProxy().setup(TranslationImpl.class, stage); extensionManager.getProxy().refineRetrieve(TranslationImpl.class, stage, builder, criteria, root, restrictions); } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Translation> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); return query.getResultList(); } finally { if (extensionManager != null) { extensionManager.getProxy().breakdown(TranslationImpl.class, stage); } } }
From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java
@Override public List<Translation> readTranslations(TranslatedEntity entity, String entityId, String fieldName) { entityId = getUpdatedEntityId(entity, entityId); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class); Root<TranslationImpl> translation = criteria.from(TranslationImpl.class); criteria.select(translation); criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()), builder.equal(translation.get("entityId"), entityId), builder.equal(translation.get("fieldName"), fieldName)); TypedQuery<Translation> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); return query.getResultList(); }
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
@Override @TransactionAttribute//from w w w . j a v a 2 s. c o m public long countByAttribute(String attributeName, Object attributeValue) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> q = cb.createQuery(Long.class); Root<T> p = q.from(persistentClass); final Expression<Long> count = newCountExpression(cb, p); q.select(count); q.where(cb.equal(p.get(attributeName), attributeValue)); try { return entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { return 0; } }
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
@Override @TransactionAttribute/*from w ww .ja v a2s .c om*/ public long countByExample(final T example, final T example2) { OpenJPACriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> q = cb.createQuery(Long.class); Root<T> from = q.from(persistentClass); final Expression<Long> count = newCountExpression(cb, from); q.select(count); q.where(newQbePredicates(cb, from, example, example2)); TypedQuery<Long> query = entityManager.createQuery(q); try { return query.getSingleResult(); } catch (NoResultException e) { return 0; } }
From source file:org.broadleafcommerce.core.catalog.dao.IndexFieldCustomPersistenceHandler.java
@Override public DynamicResultSet fetch(PersistencePackage persistencePackage, CriteriaTransferObject cto, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { FilterAndSortCriteria fieldFsc = cto.getCriteriaMap().get("field"); if (fieldFsc != null) { List<String> filterValues = fieldFsc.getFilterValues(); boolean didFilter = false; cto.getCriteriaMap().remove("field"); CriteriaBuilder builder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<IndexField> criteria = builder.createQuery(IndexField.class); Root<IndexFieldImpl> root = criteria.from(IndexFieldImpl.class); criteria.select(root); // Check if we are searching for specific field names List<Predicate> restrictions = new ArrayList<Predicate>(); if (CollectionUtils.isNotEmpty(filterValues)) { restrictions.add(builder.like(root.get("field").<String>get("friendlyName"), "%" + filterValues.get(0) + "%")); didFilter = true;/*from w w w. j a v a 2 s . c o m*/ } // Check if this filter value has a sort direction associated with it Order order = null; for (FilterAndSortCriteria sortCriteria : cto.getCriteriaMap().values()) { if (sortCriteria.getSortDirection() != null) { Path path = root; try { // Try to find the path to the property in IndexFieldImpl String[] pathParts = sortCriteria.getPropertyId().split("\\."); for (String part : pathParts) { path = path.get(part); } // If we made it here, we have the path, set the sorting (asc/desc) if (sortCriteria.getSortAscending()) { order = builder.asc(path); } else { order = builder.desc(path); } criteria.orderBy(order); break; } catch (IllegalArgumentException e) { // This isn't an actual entity property continue; } } } criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<IndexField> query = dynamicEntityDao.getStandardEntityManager().createQuery(criteria); List<IndexField> indexFields = query.getResultList(); // Convert the result list into a list of entities PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> indexFieldMetadata = helper .getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective); Entity[] entities = helper.getRecords(indexFieldMetadata, indexFields); // We need to get the total number of records available because the entityList returned may not be the full set. Map<String, FieldMetadata> originalProps = helper.getSimpleMergedProperties(IndexField.class.getName(), persistencePerspective); List<FilterMapping> filterMappings = helper.getFilterMappings(persistencePerspective, cto, IndexField.class.getName(), originalProps); int totalRecords = helper.getTotalRecords(persistencePackage.getCeilingEntityFullyQualifiedClassname(), filterMappings); // Create a Dynamic Result Set from the entity list created above. DynamicResultSet resultSet = new DynamicResultSet(entities, (didFilter ? entities.length : totalRecords)); return resultSet; } DynamicResultSet resultSet = helper.getCompatibleModule(OperationType.BASIC).fetch(persistencePackage, cto); return resultSet; }
From source file:org.jnap.core.persistence.jpa.DaoSupport.java
@Override public Long countAll() { CriteriaBuilder builder = getCriteriaBuilder(); CriteriaQuery<Long> count = builder.createQuery(Long.class); count.select(builder.count(count.from(getEntityClass()))); return entityManager.createQuery(count).getSingleResult(); }
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
@Override @TransactionAttribute/*from w ww . jav a 2 s . c o m*/ public long countByFullTexts(final String terms) { LOGGER.debug("counting by text {} terms {}", persistentClass, terms); if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) { return 0; } CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> q = cb.createQuery(Long.class); Root<T> p = q.from(persistentClass); final Expression<Long> count = newCountExpression(cb, p); q.select(count); q.where(newFullTextPredicate(cb, p, terms)); try { return entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { return 0; } }
From source file:org.broadleafcommerce.common.i18n.dao.TranslationDaoImpl.java
@Override public Translation readTranslation(TranslatedEntity entity, String entityId, String fieldName, String localeCode) {// w w w. j ava 2s . com entityId = getUpdatedEntityId(entity, entityId); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Translation> criteria = builder.createQuery(Translation.class); Root<TranslationImpl> translation = criteria.from(TranslationImpl.class); criteria.select(translation); criteria.where(builder.equal(translation.get("entityType"), entity.getFriendlyType()), builder.equal(translation.get("entityId"), entityId), builder.equal(translation.get("fieldName"), fieldName), builder.equal(translation.get("localeCode"), localeCode)); TypedQuery<Translation> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); List<Translation> translations = query.getResultList(); if (translations.size() > 1) { throw new IllegalStateException("Found multiple translations for: " + entity.getFriendlyType() + "|" + entityId + "|" + fieldName + "|" + localeCode); } if (!translations.isEmpty()) { return translations.get(0); } return null; }