List of usage examples for javax.persistence.criteria Root in
Predicate in(Object... values);
From source file:eu.uqasar.service.AbstractService.java
public <T extends Persistable> List<T> getAllExcept(Class<T> clazz, Collection<T> toExclude) { if (toExclude == null || toExclude.isEmpty()) { return getAll(clazz); }//from www . j a v a 2 s . c o m logger.infof("loading all %s except %s ...", getReadableClassName(clazz), toExclude); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> query = cb.createQuery(clazz); Root<T> root = query.from(clazz); query.where(cb.not(root.in(toExclude))); return em.createQuery(query).getResultList(); }
From source file:net.awired.generic.jpa.dao.impl.GenericDaoImpl.java
@Override public List<ENTITY> find(Collection<KEY> ids) { if (log.isDebugEnabled()) { log.debug("Finding by ids " + entityClass + "#" + ids); }//from w w w.ja v a2s . co m CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ENTITY> criteriaQuery = cb.createQuery(entityClass); Root<ENTITY> from = criteriaQuery.from(entityClass); criteriaQuery.where(from.in(ids)); TypedQuery<ENTITY> query = entityManager.createQuery(criteriaQuery); return findList(query); }
From source file:eu.uqasar.service.AbstractService.java
public List<T> getAllExcept(Collection<T> toExclude) { if (toExclude == null || toExclude.isEmpty()) { return this.getAll(); }/* w w w. ja v a 2s .c o m*/ logger.infof("loading all %s except %s ...", readableClassName, toExclude); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> query = cb.createQuery(this.clazz); Root<T> root = query.from(this.clazz); query.where(cb.not(root.in(toExclude))); return em.createQuery(query).getResultList(); }
From source file:net.groupbuy.dao.impl.ParameterDaoImpl.java
public List<Parameter> findList(ParameterGroup parameterGroup, Set<Parameter> excludes) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Parameter> criteriaQuery = criteriaBuilder.createQuery(Parameter.class); Root<Parameter> root = criteriaQuery.from(Parameter.class); criteriaQuery.select(root);/*from w w w . j a va2 s .c om*/ Predicate restrictions = criteriaBuilder.conjunction(); if (parameterGroup != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("parameterGroup"), parameterGroup)); } if (excludes != null && !excludes.isEmpty()) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList(); }
From source file:eu.uqasar.service.AbstractService.java
protected <T extends Persistable> List<T> getAllOrderedExcept(Class<T> clazz, Collection<T> toExclude, Order... orders) {// w w w .jav a2 s . c o m if (toExclude == null || toExclude.isEmpty()) { return getAll(clazz); } logger.infof("loading all %s except %s ...", getReadableClassName(clazz), toExclude); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> query = cb.createQuery(clazz); Root<T> root = query.from(clazz); if (orders != null && orders.length > 0) { query.orderBy(orders); } query.where(cb.not(root.in(toExclude))); return em.createQuery(query).getResultList(); }
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)); }/* w w w . ja va2 s .c om*/ 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.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 w w w. ja v a2 s. 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: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)); }/*from w ww .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> 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:eu.uqasar.service.user.UserService.java
public List<User> getAllExceptAndFilter(Collection<User> usersToExclude, final String filter) { if (usersToExclude == null || usersToExclude.isEmpty()) { return this.getAll(); }/*from w w w .j a v a 2 s . c o m*/ logger.infof("loading all Users except %s ...", usersToExclude); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> root = query.from(User.class); if (filter != null && !filter.isEmpty()) { Expression<String> literal = cb.upper(cb.literal(LIKE_WILDCARD + filter + LIKE_WILDCARD)); Predicate likeFirstName = cb.like(cb.upper(root.get(User_.firstName)), literal); Predicate likeLastName = cb.like(cb.upper(root.get(User_.lastName)), literal); Predicate likeUserName = cb.like(cb.upper(root.get(User_.userName)), literal); Predicate orLikeName = cb.or(likeFirstName, likeLastName, likeUserName); query.where(cb.and(cb.not(root.in(usersToExclude)), orLikeName)); } else { query.where(cb.not(root.in(usersToExclude))); } return em.createQuery(query).getResultList(); }
From source file:net.groupbuy.dao.impl.ProductDaoImpl.java
public List<Product> findList(Goods goods, Set<Product> excludes) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> criteriaQuery = criteriaBuilder.createQuery(Product.class); Root<Product> root = criteriaQuery.from(Product.class); criteriaQuery.select(root);/*from w ww . j av a 2 s.c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (goods != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("goods"), goods)); } if (excludes != null && !excludes.isEmpty()) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.not(root.in(excludes))); } criteriaQuery.where(restrictions); return entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT).getResultList(); }