List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java
protected List<Product> readFilteredActiveProductsByQueryInternal(String query, Date currentDate, SearchCriteria searchCriteria) { // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); // The root of our search is Product since we are searching Root<ProductImpl> product = criteria.from(ProductImpl.class); // We also want to filter on attributes from sku and productAttributes Join<Product, Sku> sku = product.join("defaultSku"); // Product objects are what we want back criteria.select(product); // We only want results that match the search query List<Predicate> restrictions = new ArrayList<Predicate>(); String lq = query.toLowerCase(); restrictions.add(builder.or(builder.like(builder.lower(sku.get("name").as(String.class)), '%' + lq + '%'), builder.like(builder.lower(sku.get("longDescription").as(String.class)), '%' + lq + '%'))); attachSearchCriteria(searchCriteria, product, sku, restrictions); attachActiveRestriction(currentDate, product, sku, restrictions); attachOrderBy(searchCriteria, product, sku, criteria); // Execute the query with the restrictions criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Product> typedQuery = em.createQuery(criteria); //don't cache - not really practical for open ended search return typedQuery.getResultList(); }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
/** * Builds the Typed Query//from ww w . j ava 2 s. c o m * @return */ public TypedQuery<Long> buildCount() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Long> criteria = cb.createQuery(Long.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null; //owner if (this.owner != null) { predicate = cb.equal(connection.get(Connection_.senderId), owner.getId()); } //status if (this.status != null) { if (Relationship.Type.PENDING.equals(this.status)) { predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types)); } else { predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status)); } } CriteriaQuery<Long> select = criteria.select(cb.countDistinct(connection)); select.where(predicate); return em.createQuery(select); }
From source file:se.kth.csc.persist.JPAStore.java
@Override public Iterable<Account> findAccounts(boolean onlyAdmin, String query) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Account> q = cb.createQuery(Account.class); Root<Account> account = q.from(Account.class); Expression<Boolean> expression = null; if (onlyAdmin) { // This looks like it could be replaced with account.get(Account_.admin), but it can't because of syntax expression = cb.equal(account.get(Account_.admin), true); }//from w w w. j a v a 2 s.c o m if (query != null) { Expression<Boolean> queryExpression = cb.like(cb.lower(account.get(Account_.name)), "%" + query.toLowerCase() + "%"); if (expression == null) { expression = queryExpression; } else { expression = cb.and(expression, queryExpression); } } if (expression != null) { q.where(expression); } return entityManager.createQuery(q.select(account)).getResultList(); }
From source file:org.exoplatform.social.addons.storage.dao.jpa.query.RelationshipQueryBuilder.java
/** * Builds the Typed Query/*from ww w .j av a 2s.c o m*/ * @return */ public TypedQuery<Connection> build() { EntityManager em = EntityManagerHolder.get(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Connection> criteria = cb.createQuery(Connection.class); Root<Connection> connection = criteria.from(Connection.class); Predicate predicate = null; //owner if (this.owner != null) { predicate = cb.equal(connection.get(Connection_.senderId), owner.getId()); } //status if (this.status != null) { if (Relationship.Type.PENDING.equals(this.status)) { predicate = cb.and(predicate, addInClause(cb, connection.get(Connection_.status), types)); } else { predicate = cb.and(predicate, cb.equal(connection.get(Connection_.status), this.status)); } } CriteriaQuery<Connection> select = criteria.select(connection).distinct(true); select.where(predicate); TypedQuery<Connection> typedQuery = em.createQuery(select); if (this.limit > 0) { typedQuery.setFirstResult((int) offset); typedQuery.setMaxResults((int) limit); } return typedQuery; }
From source file:org.finra.herd.dao.impl.AttributeValueListDaoImpl.java
@Override public AttributeValueListEntity getAttributeValueListByKey(AttributeValueListKey attributeValueListKey) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<AttributeValueListEntity> criteria = builder.createQuery(AttributeValueListEntity.class); // The criteria root is the attribute value list. Root<AttributeValueListEntity> attributeValueListEntityRoot = criteria.from(AttributeValueListEntity.class); // Join to the other tables we can filter on. Join<AttributeValueListEntity, NamespaceEntity> namespaceEntityJoin = attributeValueListEntityRoot .join(AttributeValueListEntity_.namespace); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); predicates.add(builder.equal(builder.upper(namespaceEntityJoin.get(NamespaceEntity_.code)), attributeValueListKey.getNamespace().toUpperCase())); predicates// w w w . ja va 2s. co m .add(builder.equal(builder.upper(attributeValueListEntityRoot.get(AttributeValueListEntity_.name)), attributeValueListKey.getAttributeValueListName().toUpperCase())); // Add all clauses to the query. criteria.select(attributeValueListEntityRoot) .where(builder.and(predicates.toArray(new Predicate[predicates.size()]))); // Execute the query and return the results. return executeSingleResultQuery(criteria, String.format( "Found more than one attribute value list with parameters {namespace=\"%s\", attribute_value_name=\"%s\"}.", attributeValueListKey.getNamespace(), attributeValueListKey.getAttributeValueListName())); }
From source file:org.apereo.portal.groups.pags.dao.jpa.JpaPersonAttributesGroupDefinitionDao.java
@Override public void afterPropertiesSet() throws Exception { this.nameParameter = this.createParameterExpression(String.class, "name"); this.findAllDefinitionsQuery = this.createCriteriaQuery( new Function<CriteriaBuilder, CriteriaQuery<PersonAttributesGroupDefinitionImpl>>() { @Override/*www. j a v a2 s . c o m*/ public CriteriaQuery<PersonAttributesGroupDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PersonAttributesGroupDefinitionImpl> criteriaQuery = cb .createQuery(PersonAttributesGroupDefinitionImpl.class); criteriaQuery.from(PersonAttributesGroupDefinitionImpl.class); return criteriaQuery; } }); this.groupDefinitionByNameQuery = this.createCriteriaQuery( new Function<CriteriaBuilder, CriteriaQuery<PersonAttributesGroupDefinitionImpl>>() { @Override public CriteriaQuery<PersonAttributesGroupDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PersonAttributesGroupDefinitionImpl> criteriaQuery = cb .createQuery(PersonAttributesGroupDefinitionImpl.class); Root<PersonAttributesGroupDefinitionImpl> root = criteriaQuery .from(PersonAttributesGroupDefinitionImpl.class); criteriaQuery.select(root).where(cb.equal(root.get("name"), nameParameter)); return criteriaQuery; } }); this.parentGroupDefinitionsQuery = this.createCriteriaQuery( new Function<CriteriaBuilder, CriteriaQuery<PersonAttributesGroupDefinitionImpl>>() { @Override public CriteriaQuery<PersonAttributesGroupDefinitionImpl> apply(CriteriaBuilder cb) { final CriteriaQuery<PersonAttributesGroupDefinitionImpl> criteriaQuery = cb .createQuery(PersonAttributesGroupDefinitionImpl.class); Root<PersonAttributesGroupDefinitionImpl> root = criteriaQuery .from(PersonAttributesGroupDefinitionImpl.class); Join<PersonAttributesGroupDefinitionImpl, PersonAttributesGroupDefinitionImpl> members = root .join(PersonAttributesGroupDefinitionImpl_.members); criteriaQuery.where( cb.equal(members.get(PersonAttributesGroupDefinitionImpl_.name), nameParameter)); return criteriaQuery; } }); }
From source file:org.broadleafcommerce.core.catalog.dao.ProductDaoImpl.java
protected List<Product> readFilteredActiveProductsByCategoryInternal(Long categoryId, Date currentDate, SearchCriteria searchCriteria) { // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); // The root of our search is Category since we are browsing Root<CategoryProductXrefImpl> productXref = criteria.from(CategoryProductXrefImpl.class); // We want to filter on attributes from product and sku Join<CategoryProductXref, Product> product = productXref.join("product"); Join<Product, Sku> sku = product.join("defaultSku"); Join<CategoryProductXref, Category> category = productXref.join("category"); // Product objects are what we want back criteria.select(product); // We only want results from the determine category List<Predicate> restrictions = new ArrayList<Predicate>(); restrictions.add(category.get("id").in(sandBoxHelper.mergeCloneIds(CategoryImpl.class, categoryId))); attachSearchCriteria(searchCriteria, product, sku, restrictions); attachActiveRestriction(currentDate, product, sku, restrictions); attachOrderBy(searchCriteria, product, sku, criteria); // Execute the query with the restrictions criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); TypedQuery<Product> typedQuery = em.createQuery(criteria); //don't cache - not really practical for open ended search //typedQuery.setHint(SandBoxHelper.QueryHints.FILTER_INCLUDE, ".*CategoryProductXrefImpl"); return typedQuery.getResultList(); }
From source file:org.finra.herd.dao.impl.AttributeValueListDaoImpl.java
@Override public List<AttributeValueListKey> getAttributeValueLists(Collection<String> namespaces) { // Create the criteria builder and the criteria. CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<AttributeValueListEntity> criteria = builder.createQuery(AttributeValueListEntity.class); // The criteria root is the attribute value list entity. Root<AttributeValueListEntity> attributeValueListEntityRoot = criteria.from(AttributeValueListEntity.class); // Join to the other tables we can filter on. Join<AttributeValueListEntity, NamespaceEntity> namespaceEntityJoin = attributeValueListEntityRoot .join(AttributeValueListEntity_.namespace); // Create the standard restrictions (i.e. the standard where clauses). List<Predicate> predicates = new ArrayList<>(); if (CollectionUtils.isNotEmpty(namespaces)) { predicates.add(namespaceEntityJoin.get(NamespaceEntity_.code).in(namespaces)); }//from w w w .j a v a2 s . c o m // Order the results by namespace and job name. List<Order> orderBy = new ArrayList<>(); orderBy.add(builder.asc(namespaceEntityJoin.get(NamespaceEntity_.code))); orderBy.add(builder.asc(attributeValueListEntityRoot.get(AttributeValueListEntity_.name))); // Add all clauses to the query. criteria.select(attributeValueListEntityRoot) .where(builder.and(predicates.toArray(new Predicate[predicates.size()]))).orderBy(orderBy); // Execute the query and build a list of keys. List<AttributeValueListKey> attributeValueListKeys = new ArrayList<>(); for (AttributeValueListEntity attributeValueListEntity : entityManager.createQuery(criteria) .getResultList()) { attributeValueListKeys.add(new AttributeValueListKey(attributeValueListEntity.getNamespace().getCode(), attributeValueListEntity.getName())); } return attributeValueListKeys; }
From source file:org.openlmis.migration.tool.openlmis.referencedata.repository.custom.impl.OlmisFacilityTypeApprovedProductRepositoryImpl.java
@Override public Collection<FacilityTypeApprovedProduct> searchProducts(UUID facilityId, UUID programId, boolean fullSupply) { checkNotNull(facilityId);/*from w ww.j ava2s. co m*/ CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<FacilityTypeApprovedProduct> query = builder.createQuery(FacilityTypeApprovedProduct.class); Root<FacilityTypeApprovedProduct> ftap = query.from(FacilityTypeApprovedProduct.class); Root<Facility> facility = query.from(Facility.class); Join<Facility, FacilityType> fft = facility.join("type"); Join<FacilityTypeApprovedProduct, FacilityType> ft = ftap.join("facilityType"); Join<FacilityTypeApprovedProduct, ProgramOrderable> pp = ftap.join("programOrderable"); Join<ProgramOrderable, Program> program = pp.join("program"); Predicate conjunction = builder.conjunction(); if (programId != null) { conjunction = builder.and(conjunction, builder.equal(program.get("id"), programId)); } conjunction = builder.and(conjunction, builder.equal(fft.get("id"), ft.get("id"))); conjunction = builder.and(conjunction, builder.equal(facility.get("id"), facilityId)); conjunction = builder.and(conjunction, builder.equal(pp.get("fullSupply"), fullSupply)); conjunction = builder.and(conjunction, builder.isTrue(pp.get("active"))); query.select(ftap); query.where(conjunction); Join<ProgramOrderable, OrderableDisplayCategory> category = pp.join("orderableDisplayCategory"); Join<ProgramOrderable, Orderable> orderable = pp.join("product"); query.orderBy(builder.asc(category.get("orderedDisplayValue").get("displayOrder")), builder.asc(category.get("orderedDisplayValue").get("displayName")), builder.asc(orderable.get("productCode"))); return entityManager.createQuery(query).getResultList(); }