List of usage examples for javax.persistence.criteria Root get
<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find a list of entities of the given type, using as filter criteria the * specified list of attribute values.//www .j a v a 2 s. c o m * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> List<T> findEntitiesByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (List<T>) query.getResultList(); }
From source file:com.sixsq.slipstream.persistence.Run.java
public static List<RunView> viewList(User user, String moduleResourceUri, Integer offset, Integer limit, String cloudServiceName) throws ConfigurationException, ValidationException { List<RunView> views = null; EntityManager em = PersistenceUtil.createEntityManager(); try {/*from w ww.j ava 2 s .c o m*/ CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Run> critQuery = builder.createQuery(Run.class); Root<Run> rootQuery = critQuery.from(Run.class); critQuery.select(rootQuery); Predicate where = viewListCommonQueryOptions(builder, rootQuery, user, moduleResourceUri, cloudServiceName); if (where != null) { critQuery.where(where); } critQuery.orderBy(builder.desc(rootQuery.get("startTime"))); TypedQuery<Run> query = em.createQuery(critQuery); if (offset != null) { query.setFirstResult(offset); } query.setMaxResults((limit != null) ? limit : DEFAULT_LIMIT); List<Run> runs = query.getResultList(); views = convertRunsToRunViews(runs); } finally { em.close(); } return views; }
From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java
@Override public Specification<Device> forManufacturer(final Manufacturer manufacturer) throws ArgumentNullOrEmptyException { if (manufacturer == null) { throw new ArgumentNullOrEmptyException("manufacturer"); }// w w w . j a v a2 s . c o m return new Specification<Device>() { @Override public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query, final CriteriaBuilder cb) { final Subquery<Long> subquery = query.subquery(Long.class); final Root<DeviceModel> deviceModelRoot = subquery.from(DeviceModel.class); subquery.select(deviceModelRoot.get("id").as(Long.class)); subquery.where(cb.equal(cb.upper(deviceModelRoot.get("manufacturerId").<String>get("name")), manufacturer.getName().toUpperCase())); return cb.in(deviceRoot.get("deviceModel").get("id").as(Long.class)).value(subquery); } }; }
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 www . j av a 2s.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:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * Adds the filter criteria./*from w w w . j a va 2s . co m*/ * * @param userId * the user id * @param filters * the filters * @param builder * the builder * @param root * the root * @param query * the query */ private void addFilterCriteria(long userId, Map<String, Object> filters, CriteriaBuilder builder, Root<Transaction> root, CriteriaQuery query) { List<Predicate> predicates = new ArrayList<Predicate>(); if (!filters.isEmpty()) { for (Entry<String, Object> entry : filters.entrySet()) { if (CommonUtil.isNotNull(root.get(entry.getKey()))) { predicates.add(builder.like(root.<String>get(entry.getKey()), entry.getValue() + MODULO)); } } } if (CommonUtil.isNotNull(userId)) { User user = userRepository.findById(userId); if (CommonUtil.isNotNull(user) && user.getRole().equals(ROLE_CUSTOMER)) { predicates.add(builder.equal(root.<Long>get(FIELDCONSTANT_USERID), user.getId())); } } query.where(predicates.toArray(new Predicate[] {})); }
From source file:cl.troncador.delfin.query.constraint.PrepareOrderAdapter.java
public Order[] getOrderArray(Root<T> root, CriteriaBuilder criteriaBuilder) { List<Order> orderList = new ArrayList<Order>(); for (Pair<SingularAttribute<T, ?>, Direction> directionedColumn : directionedColumnList) { Direction direction = directionedColumn.getRight(); SingularAttribute<T, ?> column = directionedColumn.getLeft(); Path<?> path = root.get(column); Order order = null;/*from w ww. j ava2 s.c om*/ switch (direction) { case ASC: order = criteriaBuilder.asc(path); break; case DES: order = criteriaBuilder.desc(path); break; } orderList.add(order); } return orderList.toArray(new Order[orderList.size()]); }
From source file:eu.uqasar.service.ProcessService.java
public List<Process> getAllByAscendingEndDateNameFiltered(ProcessesFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Process> criteria = cb.createQuery(Process.class); Root<Process> root = criteria.from(Process.class); List<Predicate> predicates = getFilterPredicates(filter, cb, root); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }//from w ww . ja v a2s . c o m criteria.orderBy(cb.asc(root.get(Process_.endDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:eu.uqasar.service.ProcessService.java
public List<Process> getAllAscendingStartDateNameFiltered(ProcessesFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Process> criteria = cb.createQuery(Process.class); Root<Process> root = criteria.from(Process.class); List<Predicate> predicates = getFilterPredicates(filter, cb, root); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }//from ww w .ja v a 2 s . c o m criteria.orderBy(cb.asc(root.get(Process_.startDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:eu.uqasar.service.ProcessService.java
public List<Process> getAllByDescendingEndDateNameFiltered(ProcessesFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Process> criteria = cb.createQuery(Process.class); Root<Process> root = criteria.from(Process.class); List<Predicate> predicates = getFilterPredicates(filter, cb, root); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }/* w ww . j a va 2 s. c om*/ criteria.orderBy(cb.desc(root.get(Process_.endDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:eu.uqasar.service.ProcessService.java
public List<Process> getAllByDescendingStartDateNameFiltered(ProcessesFilterStructure filter, int first, int count) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Process> criteria = cb.createQuery(Process.class); Root<Process> root = criteria.from(Process.class); List<Predicate> predicates = getFilterPredicates(filter, cb, root); if (!predicates.isEmpty()) { criteria.where(cb.and(predicates.toArray(new Predicate[predicates.size()]))); }//from w w w .j av a 2 s . c o m criteria.orderBy(cb.desc(root.get(Process_.startDate))); return em.createQuery(criteria).setFirstResult(first).setMaxResults(count).getResultList(); }