List of usage examples for javax.persistence.criteria Subquery select
Subquery<T> select(Expression<T> expression);
From source file:net.groupbuy.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, List<Tag> tags, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);/*from ww w .java 2s . com*/ Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), true)); if (articleCategory != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.equal(root.get("articleCategory"), articleCategory), criteriaBuilder.like(root.get("articleCategory").<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); } if (tags != null && !tags.isEmpty()) { Subquery<Article> subquery = criteriaQuery.subquery(Article.class); Root<Article> subqueryRoot = subquery.from(Article.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.equal(subqueryRoot, root), subqueryRoot.join("tags").in(tags)); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.exists(subquery)); } criteriaQuery.where(restrictions); criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop"))); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:dao.jpa.TestJpaDao.java
@Test @Transactional/*w w w . jav a 2 s.c o m*/ public void testCopy() { EntityManager em = bookDao.getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> criteria = cb.createQuery(Book.class); // Fetch join Root<Book> root = criteria.from(Book.class); Path<String> path = root.join("author").<String>get("name"); root.fetch("author"); criteria.select(root); // SubQuery Subquery<String> sq = criteria.subquery(String.class); Root<Author> author = sq.from(Author.class); sq.select(author.<String>get("name")); sq.where(cb.equal(author.<String>get("name"), "Rod")); criteria.where(cb.in(path).value(sq)); CriteriaQuery<Book> copy = cb.createQuery(Book.class); JpaUtils.copyCriteria(criteria, copy); List<Book> copyBooks = em.createQuery(copy).getResultList(); List<Book> books = em.createQuery(criteria).getResultList(); assertEquals(books, copyBooks); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//from w ww . jav a 2 s . co m Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } criteriaQuery.where(restrictions); if (orders == null || orders.isEmpty()) { criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public Page<Article> findPage(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Pageable pageable) {//from w w w . j a va 2 s .co m CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } criteriaQuery.where(restrictions); if (pageable == null || ((StringUtils.isEmpty(pageable.getOrderProperty()) || pageable.getOrderDirection() == null) && CollectionUtils.isEmpty(pageable.getOrders()))) { criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findPage(criteriaQuery, pageable); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Boolean isPublication, Article.GenerateMethod generateMethod, Date beginDate, Date endDate, Integer first, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);//from w ww . j a va 2 s .c o m Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } if (generateMethod != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("generateMethod"), generateMethod)); } if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("createDate"), beginDate)); } if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(root.<Date>get("createDate"), endDate)); } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, first, count, null, null); }
From source file:com.expressui.core.dao.security.query.RelatedUsersQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(role)) { ParameterExpression<Role> roleExp = builder.parameter(Role.class, "role"); predicates.add(builder.equal(user.join("userRoles").get("role"), roleExp)); }//www . j a v a 2 s. co m if (hasValue(doesNotBelongToRole)) { Subquery<User> subquery = query.subquery(User.class); Root userRole = subquery.from(UserRole.class); ParameterExpression<Role> roleExp = builder.parameter(Role.class, "doesNotBelongToRole"); subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), roleExp)); predicates.add(builder.not(user.in(subquery))); } return predicates; }
From source file:com.expressui.core.dao.security.query.UserQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<User> query, Root<User> user) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(loginName)) { ParameterExpression<String> loginNameExp = builder.parameter(String.class, "loginName"); predicates.add(builder.like(builder.upper(user.<String>get("loginName")), loginNameExp)); }// w w w. j ava2 s .c o m if (hasValue(doesNotBelongToRole)) { Subquery<User> subquery = query.subquery(User.class); Root userRole = subquery.from(UserRole.class); ParameterExpression<Role> role = builder.parameter(Role.class, "doesNotBelongToRole"); subquery.select(userRole.get("user")).where(builder.equal(userRole.get("role"), role)); predicates.add(builder.not(user.in(subquery))); } return predicates; }
From source file:com.expressui.core.dao.security.query.RoleQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Role> query, Root<Role> role) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(name)) { ParameterExpression<String> nameExp = builder.parameter(String.class, "name"); predicates.add(builder.like(builder.upper(role.<String>get("name")), nameExp)); }/*from www .j ava 2s .c om*/ if (hasValue(doesNotBelongToUser)) { Subquery<Role> subquery = query.subquery(Role.class); Root<UserRole> userRole = subquery.from(UserRole.class); ParameterExpression<User> userExp = builder.parameter(User.class, "doesNotBelongToUser"); subquery.select(userRole.<Role>get("role")).where(builder.equal(userRole.get("user"), userExp)); predicates.add(builder.not(role.in(subquery))); } return predicates; }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
public <T extends ResourceEntity> SearchResult<T> search(Class<T> clazz, ParseTree filterTree, int count, int startIndex, String sortBy, String sortOrder, FilterParser<T> filterParser) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> resourceQuery = cb.createQuery(clazz); Root<T> resourceRoot = resourceQuery.from(clazz); Subquery<Long> internalIdQuery = resourceQuery.subquery(Long.class); Root<T> internalIdRoot = internalIdQuery.from(clazz); internalIdQuery.select(internalIdRoot.get(ResourceEntity_.internalId)); if (filterTree != null && filterTree.getChildCount() > 0) { Predicate predicate = filterParser.createPredicateAndJoin(filterTree, internalIdRoot); internalIdQuery.where(predicate); }/*from www . j av a 2 s .c om*/ resourceQuery.select(resourceRoot) .where(cb.in(resourceRoot.get(ResourceEntity_.internalId)).value(internalIdQuery)); // TODO: evaluate if a User-/GroupDao supplied default sortBy field is possible Expression<?> sortByField = resourceRoot.get(ResourceEntity_.id); if (sortBy != null && !sortBy.isEmpty()) { sortByField = filterParser.createSortByField(sortBy, resourceRoot); } // default order is ascending Order order = cb.asc(sortByField); if (sortOrder.equalsIgnoreCase("descending")) { order = cb.desc(sortByField); } resourceQuery.orderBy(order); TypedQuery<T> query = em.createQuery(resourceQuery); query.setFirstResult(startIndex); query.setMaxResults(count); List<T> results = query.getResultList(); long totalResult = getTotalResults(clazz, internalIdQuery); return new SearchResult<>(results, totalResult); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercilessly() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override/*from www . j a v a 2 s. c o m*/ public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Product> product = query.from(Product.class); query.select(product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(product.get(Product_.name)), "%tv%")); Subquery<Long> subQuery = query.subquery(Long.class); Root<Image> infoRoot = subQuery.from(Image.class); Join<Image, Product> productJoin = infoRoot.join(Image_.product); subQuery.select(productJoin.<Long>get(Product_.id)); subQuery.where(cb.gt(infoRoot.get(Image_.index), 0)); criteria.add(cb.in(product.get(Product_.id)).value(subQuery)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }