List of usage examples for javax.persistence.criteria CriteriaQuery where
CriteriaQuery<T> where(Predicate... restrictions);
From source file:com.sfs.ucm.controller.SpecificationAction.java
/** * Load resources/* www . j ava 2s .c om*/ * * @param project */ private void loadFeatures(final Project project) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Feature> c = cb.createQuery(Feature.class); Root<Feature> obj = c.from(Feature.class); c.select(obj); c.where(cb.equal(obj.get("project"), project)); c.orderBy(cb.asc(obj.get("id"))); this.features = em.createQuery(c).getResultList(); }
From source file:com.qpark.eip.core.spring.auth.dao.AuthorityDao.java
/** * Get the {@link AuthenticationType}s out of the database. * * @param enabled/*from w ww .jav a2s.co m*/ * if not <code>null</code> and <code>true</code> only the * enabled {@link AuthenticationType}s are replied. * @return the list of {@link AuthenticationType}s. */ @Transactional(value = EipPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED) public List<AuthenticationType> getAuthenticationTypes(final Boolean enabled) { CriteriaBuilder cb = this.em.getCriteriaBuilder(); CriteriaQuery<AuthenticationType> q = cb.createQuery(AuthenticationType.class); Root<AuthenticationType> c = q.from(AuthenticationType.class); Predicate ands = cb.conjunction(); ands.getExpressions().add(cb.equal(c.<String>get(AuthenticationType_.context), this.getContextName())); if (enabled != null) { ands.getExpressions().add(cb.equal(c.<Boolean>get(AuthenticationType_.enabled), enabled)); } q.where(ands); q.orderBy(cb.asc(c.<String>get(AuthenticationType_.userName))); TypedQuery<AuthenticationType> typedQuery = this.em.createQuery(q); List<AuthenticationType> list = typedQuery.getResultList(); for (AuthenticationType auth : list) { for (GrantedAuthorityType gr : auth.getGrantedAuthority()) { gr.getRoleName(); } for (int i = 0; i < auth.getGrantedAuthority().size(); i++) { // eager loading... auth.getGrantedAuthority().get(i); } } return list; }
From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java
/** * Adaptation of the algorithm defined in the GraphQL specification. * <p>/*w w w . ja v a 2s . c o m*/ * Please look at the following link for more details: * https://facebook.github.io/relay/graphql/connections.htm#sec-Pagination-algorithm * <p> * The opaque cursor that is returned to the client makes use of the entity ID internally. */ protected ExtendedConnection createPaginatedConnection(DataFetchingEnvironment environment, Class<E> entityClass, Function<Root<E>, Path<? extends Number>> entityId, Comparator<E> entityComparator, BiFunction<CriteriaBuilder, Root<E>, List<Predicate[]>> criteria, CursorMapper<T, Integer> cursorMapper) { Integer first = environment.getArgument(FIRST.getName()); Integer last = environment.getArgument(LAST.getName()); Integer after = cursorMapper.getOffsetFromCursor(environment.getArgument(AFTER.getName())); Integer before = cursorMapper.getOffsetFromCursor(environment.getArgument(BEFORE.getName())); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(entityClass); Root<E> entityRoot = criteriaQuery.from(entityClass); Path<? extends Number> entityIdPath = entityId.apply(entityRoot); Predicate cursorPredicate = createCursorPredicate(criteriaBuilder, entityIdPath, after, before); int maxResults = applySlicing(criteriaQuery, criteriaBuilder, entityIdPath, first, last); CriteriaQuery<E> select = criteriaQuery.select(entityRoot); List<Predicate[]> predicates = criteria.apply(criteriaBuilder, entityRoot); Predicate[] wherePredicate = buildWherePredicate(predicates, cursorPredicate, criteriaBuilder); if (wherePredicate.length > 0) { select.where(wherePredicate); } TypedQuery<E> query = entityManager.createQuery(select); if (maxResults > -1) { query.setMaxResults(maxResults); } Stream<E> dataStream = query.getResultList().stream(); // if last is provided, reverse the stream // in order to get results sorted in ascending order based on entities ID if (last != null) { dataStream = dataStream.sorted(entityComparator); } Stream<T> data = dataMapping(dataStream); ExtendedConnection connection = createRelayConnection(entityManager, entityClass, criteriaBuilder, wherePredicate, cursorMapper, data, first, last); return connection; }
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);// w w w .j a v a 2 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:org.apache.rave.portal.repository.impl.JpaWidgetRepository.java
@Override public List<Widget> getByStatusAndTypeAndFreeTextSearch(WidgetStatus widgetStatus, String type, String searchTerm, int offset, int pageSize) { final CriteriaBuilder cb = manager.getCriteriaBuilder(); final CriteriaQuery<JpaWidget> query = cb.createQuery(JpaWidget.class); final Root<JpaWidget> widgetType = query.from(JpaWidget.class); query.where(getStatusAndTypeAndFreeTextPredicates(cb, widgetType, widgetStatus, type, searchTerm)); query.orderBy(getOrderByTitleAsc(cb, widgetType)); return expandProperties(getPagedResultList(manager.createQuery(query), offset, pageSize)); }
From source file:org.easy.criteria.BaseDAO.java
/** * @param forClass/* w w w .j a v a 2 s . c o m*/ * @param attributeToMatch * @param value * @return */ public <E, V> List<E> findByProperty(Class<E> forClass, SingularAttribute<E, V> attributeToMatch, V value) { if (log.isTraceEnabled()) log.trace("findByProperty: "); Preconditions.checkNotNull(forClass); CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder(); CriteriaQuery<E> criteria = criteriaBuilder.createQuery(forClass); log.debug("Root :" + forClass.getSimpleName()); Root<E> child = criteria.from(forClass); criteria.distinct(true); Predicate predicate = criteriaBuilder.equal(child.get(attributeToMatch), value); criteria.where(predicate); TypedQuery<E> query = _entityManager.createQuery(criteria); return query.getResultList(); }
From source file:br.com.sementesdoamanha.repository.Servidores.java
public List<Servidor> filtrados(ServidorFilter filtro) { //select, from, where, like... --> select(), from(), where() //JPQL: from Servidor //JPQL: select s from Servidor s where c.nome like = 'Joo%' and c.cpf = like '046.244.901-77' CriteriaBuilder builder = manager.getCriteriaBuilder(); CriteriaQuery<Servidor> criteriaQuery = builder.createQuery(Servidor.class); Root<Servidor> s = criteriaQuery.from(Servidor.class); criteriaQuery.select(s);//from w w w . ja va 2 s . c o m List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(filtro.getCpf())) { predicates.add(builder.equal(s.<String>get("cpf"), filtro.getCpf())); } if (StringUtils.isNotBlank(filtro.getNome())) { predicates .add(builder.like(builder.upper(s.<String>get("nome")), filtro.getNome().toUpperCase() + "%")); } criteriaQuery.where(predicates.toArray(new Predicate[0])); TypedQuery<Servidor> query = manager.createQuery(criteriaQuery); return query.getResultList(); }
From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceGroupRepository.java
/** * * @param name/* w w w . ja v a 2s . c o m*/ * @param resourceTypeId * @return */ public ResourceGroupEntity loadUniqueGroupByNameAndType(String name, Integer resourceTypeId) { ResourceGroupEntity result = null; try { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ResourceGroupEntity> q = cb.createQuery(ResourceGroupEntity.class); Root<ResourceGroupEntity> r = q.from(ResourceGroupEntity.class); r.fetch("resources"); Join<ResourceGroupEntity, ResourceEntity> resources = r.join("resources"); Predicate typePred = cb.equal(resources.get("resourceType").get("id"), resourceTypeId); Predicate resNamePred = cb.equal(resources.get("name"), name); q.where(cb.and(typePred, resNamePred)); q.distinct(true); result = entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { // do nothing } return result; }
From source file:org.businessmanager.dao.GenericDaoImpl.java
@Override public Long getCount(Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) { CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = queryBuilder.createQuery(Long.class); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<Long> select = criteriaQuery.select(queryBuilder.count(rootQuery)); List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder, rootQuery);/* w ww . jav a 2 s . c o m*/ criteriaQuery.where(predicateList.toArray(new Predicate[0])); TypedQuery<Long> typedQuery = entityManager.createQuery(select); return typedQuery.getSingleResult(); }
From source file:dao.jpa.TestJpaDao.java
@Test @Transactional//from w ww . j a v a 2s .com 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); }