List of usage examples for javax.persistence.criteria Root join
<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute);
From source file:org.oncoblocks.centromere.jpa.QueryCriteriaSpecification.java
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) { String key = queryCriteria.getKey(); Object value = queryCriteria.getValue(); Evaluation eval = queryCriteria.getEvaluation(); Path path = null;//from www . j a va 2 s. co m if (key.contains(".")) { String[] bits = key.split("\\."); path = root.join(bits[0]).get(bits[1]); } else { path = root.get(key); } logger.debug(String.format("[CENTROMERE] Converting QueryCriteria to JPA specification: %s", queryCriteria.toString())); switch (eval) { case EQUALS: return criteriaBuilder.equal(path, value); case NOT_EQUALS: return criteriaBuilder.notEqual(path, value); case IN: return path.in(value); case NOT_IN: return criteriaBuilder.not(path.in(value)); case IS_NULL: return criteriaBuilder.isNull(path); case NOT_NULL: return criteriaBuilder.isNotNull(path); case GREATER_THAN: return criteriaBuilder.greaterThan(path, value.toString()); case GREATER_THAN_EQUALS: return criteriaBuilder.greaterThanOrEqualTo(path, value.toString()); case LESS_THAN: return criteriaBuilder.lessThan(path, value.toString()); case LESS_THAN_EQUALS: return criteriaBuilder.lessThanOrEqualTo(path, value.toString()); case BETWEEN: return criteriaBuilder.and(criteriaBuilder.greaterThan(path, ((List<?>) value).get(0).toString()), criteriaBuilder.lessThan(path, ((List<?>) value).get(1).toString())); case OUTSIDE: return criteriaBuilder.or(criteriaBuilder.greaterThan(path, ((List<?>) value).get(1).toString()), criteriaBuilder.lessThan(path, ((List<?>) value).get(0).toString())); case BETWEEN_INCLUSIVE: return criteriaBuilder.and( criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(0).toString()), criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(1).toString())); case OUTSIDE_INCLUSIVE: return criteriaBuilder.or( criteriaBuilder.greaterThanOrEqualTo(path, ((List<?>) value).get(1).toString()), criteriaBuilder.lessThanOrEqualTo(path, ((List<?>) value).get(0).toString())); case LIKE: return criteriaBuilder.like(path, "%" + value.toString() + "%"); case NOT_LIKE: return criteriaBuilder.notLike(path, "%" + value.toString() + "%"); case STARTS_WITH: return criteriaBuilder.like(path, value.toString() + "%"); case ENDS_WITH: return criteriaBuilder.like(path, "%" + value.toString()); default: return criteriaBuilder.equal(root.get(queryCriteria.getKey()), queryCriteria.getValue()); } }
From source file:me.ineson.demo.service.utils.RestUtils.java
/** * @param where/*from w ww . j a v a 2 s. com*/ * @param root * @param query * @param builder * @param translations * @return */ public static Predicate parseWhereClause(String where, Root<?> root, CriteriaQuery<?> query, CriteriaBuilder builder, Map<String, String> translations) { List<Predicate> predicates = new ArrayList<Predicate>(); for (String singleCriteria : new StrTokenizer(where, ",").getTokenList()) { if (StringUtils.isNotBlank(singleCriteria)) { int equalsIndex = singleCriteria.indexOf("="); if (equalsIndex > 0) { String fieldPath = singleCriteria.substring(0, equalsIndex); String value = singleCriteria.substring(equalsIndex + 1); if (translations != null && translations.containsKey(fieldPath)) { String newFieldPath = translations.get(fieldPath); log.debug("replacing field {} with {} ", fieldPath, newFieldPath); fieldPath = newFieldPath; } StrTokenizer tokenizer = new StrTokenizer(fieldPath, "."); javax.persistence.criteria.Path<?> expression = null; while (tokenizer.hasNext()) { String field = tokenizer.next(); if (tokenizer.hasNext()) { if (expression == null) { expression = root.join(field); } else { // expression = expression.join( field); throw new IllegalArgumentException( "Paths to joins of greater than a depth of 1 are not implemented yet"); } } else { if (expression == null) { log.info("expression0 {}", expression); expression = root.get(field); log.info("expression1 {}", expression); } else { expression = expression.get(field); } } } Object realValue = value; if ("bodyType".equals(fieldPath)) { me.ineson.demo.service.SolarBodyType solarBodyType = me.ineson.demo.service.SolarBodyType .valueOf(value); switch (solarBodyType) { case PLANET: realValue = SolarBodyType.Planet; break; case SUN: realValue = SolarBodyType.Sun; break; case DWARF_PLANET: realValue = SolarBodyType.DwarfPlanet; break; default: realValue = solarBodyType; } log.info("enum bodyType before {} after {}", value, realValue); } log.info("expression9 {}", expression); predicates.add(builder.equal(expression, realValue)); } } } log.debug("predictes "); if (predicates.size() == 0) { return null; } if (predicates.size() == 1) { return predicates.get(0); } return builder.and(predicates.toArray(new Predicate[predicates.size()])); }
From source file:de.whs.poodle.repositories.StatisticsRepository.java
public List<Statistic> getStatistics(FeedbackSearchCriteria s, int instructorId, int limit) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Statistic> cq = cb.createQuery(Statistic.class); Root<Statistic> statistic = cq.from(Statistic.class); /* fetch the exercises immediately so they don't have to * be lazy-fetched one by one while rendering the table rows. */ statistic.fetch("exercise"); // join we need to match the course Join<Statistic, Course> course = statistic.join("courseTerm").join("course"); // create empty "and" predicate Predicate where = cb.conjunction(); // only non-empty Predicate notEmpty = cb.isFalse(statistic.get("empty")); where = cb.and(where, notEmpty);//from ww w . j a va2s. com // only statistics for courses that the instructor has access to Predicate hasInstructorAccessToCourse = cb.isTrue(cb.function("has_instructor_access_to_course", Boolean.class, course.get("id"), cb.literal(instructorId))); where = cb.and(where, hasInstructorAccessToCourse); // filter by courseId if (s.getCourseId() != 0) { Predicate courseMatches = cb.equal(course.get("id"), s.getCourseId()); where = cb.and(where, courseMatches); } // filter by student if (s.getStudent() != null) { Predicate studentMatches = cb.equal(statistic.get("student").get("id"), s.getStudent().getId()); where = cb.and(where, studentMatches); } cq.where(where); // order by date cq.orderBy(cb.desc(statistic.get("completedAt"))); return em.createQuery(cq).setMaxResults(limit).getResultList(); }
From source file:org.openregistry.core.repository.jpa.JpaPersonRepository.java
public List<Person> searchByCriteria(final SearchCriteria searchCriteria) throws RepositoryAccessException { final String givenName = searchCriteria.getGivenName(); final String familyName = searchCriteria.getFamilyName(); final Date birthDate = searchCriteria.getDateOfBirth(); final String searchCriteriaName = searchCriteria.getName(); // search by role criteria final String sponsorNetID = searchCriteria.getSponsorNetID(); final OrganizationalUnit organizationalUnit = searchCriteria.getOrganizationalUnit(); final Date roleExpDate = searchCriteria.getRoleExpDate(); final Type roleType = searchCriteria.getAffiliationType(); final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery<JpaPersonImpl> c = criteriaBuilder.createQuery(JpaPersonImpl.class); c.distinct(true);//from w ww . j a v a 2 s . co m final Root<JpaPersonImpl> person = c.from(JpaPersonImpl.class); final Join<JpaPersonImpl, JpaNameImpl> name = person.join(JpaPersonImpl_.names); final Join<JpaPersonImpl, JpaRoleImpl> role = person.join(JpaPersonImpl_.roles); // person.fetch(JpaPersonImpl_.names); // person.fetch(JpaPersonImpl_.roles); // person.fetch(JpaPersonImpl_.identifiers); final Predicate pBirthDate; if (birthDate != null) { pBirthDate = criteriaBuilder.equal(person.get(JpaPersonImpl_.dateOfBirth), birthDate); } else { pBirthDate = null; } Predicate combined = null; if (StringUtils.hasText(givenName) && StringUtils.hasText(familyName)) { // final Predicate pGivenName = criteriaBuilder.equal(name.get(JpaNameImpl_.given), givenName ); Expression<String> pattern = criteriaBuilder.literal((String) givenName + "%"); final Predicate pGivenName = criteriaBuilder.like(name.get(JpaNameImpl_.given), pattern); final Predicate pFamilyName = criteriaBuilder.equal(name.get(JpaNameImpl_.family), familyName); combined = criteriaBuilder.and(pGivenName, pFamilyName); } else if (StringUtils.hasText(givenName)) { combined = criteriaBuilder.equal(name.get(JpaNameImpl_.given), givenName); } else if (StringUtils.hasText(familyName)) { combined = criteriaBuilder.equal(name.get(JpaNameImpl_.family), familyName); } else if (StringUtils.hasText(searchCriteriaName)) { final Predicate pGivenName = criteriaBuilder.equal(name.get(JpaNameImpl_.given), searchCriteriaName); final Predicate pFamilyName = criteriaBuilder.equal(name.get(JpaNameImpl_.family), searchCriteriaName); combined = criteriaBuilder.or(pGivenName, pFamilyName); } Predicate pRoleCombined = null; if (roleType != null) { //final Join<JpaPersonImpl,JpaRoleImpl> role = person.join(JpaPersonImpl_.roles); //final Join<JpaPersonImpl,JpaRoleImpl> role = name.join(JpaPersonImpl_.roles); pRoleCombined = criteriaBuilder.equal(role.get(JpaRoleImpl_.affiliationType), roleType); } if (organizationalUnit != null && StringUtils.hasText(organizationalUnit.getName())) { Predicate orgUnitP = criteriaBuilder.equal(role.get(JpaRoleImpl_.organizationalUnit), organizationalUnit); if (pRoleCombined != null) pRoleCombined = criteriaBuilder.and(pRoleCombined, orgUnitP); else pRoleCombined = orgUnitP; //pRoleCombined = criteriaBuilder.and(pRoleCombined, orgUnitP); } if (StringUtils.hasText(sponsorNetID)) { Person person1 = findByIdentifier(Type.IdentifierTypes.NETID.name(), sponsorNetID); if (person1 != null) { Predicate sponsorP = criteriaBuilder.equal(role.get(JpaRoleImpl_.sponsorId), person1.getId()); if (pRoleCombined != null) pRoleCombined = criteriaBuilder.and(pRoleCombined, sponsorP); else pRoleCombined = sponsorP; } else { pRoleCombined = criteriaBuilder.or(); } } if (roleExpDate != null) { Predicate expDateP = criteriaBuilder.between(role.get(JpaRoleImpl_.end), new Date(), roleExpDate); if (pRoleCombined != null) pRoleCombined = criteriaBuilder.and(pRoleCombined, expDateP); else pRoleCombined = expDateP; } Predicate pComplete = criteriaBuilder.and(); if (pBirthDate != null) pComplete = criteriaBuilder.and(pComplete, pBirthDate); if (combined != null) pComplete = criteriaBuilder.and(pComplete, combined); if (pRoleCombined != null) pComplete = criteriaBuilder.and(pComplete, pRoleCombined); c.select(person).where(pComplete); // if (pBirthDate != null && combined != null) { // c.select(person).where(criteriaBuilder.and(pBirthDate, combined)); // } else if (pBirthDate != null) { // c.select(person).where(pBirthDate); // } else { // c.select(person).where(combined); // } final List<JpaPersonImpl> persons = this.entityManager.createQuery(c).setMaxResults(MAX_QUERY_LIMIT) .getResultList(); return new ArrayList<Person>(persons); }
From source file:ch.puzzle.itc.mobiliar.business.resourcerelation.control.ResourceRelationService.java
public List<ConsumedResourceRelationEntity> getConsumedSlaveRelations(ResourceEntity slaveResource) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ConsumedResourceRelationEntity> q = cb.createQuery(ConsumedResourceRelationEntity.class); Root<ConsumedResourceRelationEntity> r = q.from(ConsumedResourceRelationEntity.class); Join<ConsumedResourceRelationEntity, ResourceEntity> slaveResourceJoin = r.join("slaveResource"); q.where(cb.equal(slaveResourceJoin.get("id"), slaveResource.getId())); TypedQuery<ConsumedResourceRelationEntity> query = entityManager.createQuery(q); return query.getResultList(); }
From source file:ch.puzzle.itc.mobiliar.business.resourcerelation.control.ResourceRelationService.java
public List<ProvidedResourceRelationEntity> getProvidedSlaveRelations(ResourceEntity slaveResource) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ProvidedResourceRelationEntity> q = cb.createQuery(ProvidedResourceRelationEntity.class); Root<ProvidedResourceRelationEntity> r = q.from(ProvidedResourceRelationEntity.class); Join<ProvidedResourceRelationEntity, ResourceEntity> slaveResourceJoin = r.join("slaveResource"); q.where(cb.equal(slaveResourceJoin.get("id"), slaveResource.getId())); TypedQuery<ProvidedResourceRelationEntity> query = entityManager.createQuery(q); return query.getResultList(); }
From source file:net.groupbuy.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, net.groupbuy.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }/*from w ww . j a v a 2 s.co m*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:com.dp2345.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, com.dp2345.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }//from w w w . j a v a 2s . co m if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.osxx.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, net.osxx.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }/*from w ww . j ava 2 s . com*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }
From source file:net.shopxx.dao.impl.ProductDaoImpl.java
public List<Object[]> findSalesList(Date beginDate, Date endDate, Integer count) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Object[]> criteriaQuery = criteriaBuilder.createQuery(Object[].class); Root<Product> product = criteriaQuery.from(Product.class); Join<Product, OrderItem> orderItems = product.join("orderItems"); Join<Product, net.shopxx.entity.Order> order = orderItems.join("order"); criteriaQuery.multiselect(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price"), criteriaBuilder.sum(orderItems.<Integer>get("quantity")), criteriaBuilder.sum(criteriaBuilder .prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price")))); Predicate restrictions = criteriaBuilder.conjunction(); if (beginDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.greaterThanOrEqualTo(order.<Date>get("createDate"), beginDate)); }/*from w w w. j a v a 2s. com*/ if (endDate != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.lessThanOrEqualTo(order.<Date>get("createDate"), endDate)); } restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(order.get("orderStatus"), OrderStatus.completed), criteriaBuilder.equal(order.get("paymentStatus"), PaymentStatus.paid)); criteriaQuery.where(restrictions); criteriaQuery.groupBy(product.get("id"), product.get("sn"), product.get("name"), product.get("fullName"), product.get("price")); criteriaQuery.orderBy(criteriaBuilder.desc(criteriaBuilder.sum( criteriaBuilder.prod(orderItems.<Integer>get("quantity"), orderItems.<BigDecimal>get("price"))))); TypedQuery<Object[]> query = entityManager.createQuery(criteriaQuery).setFlushMode(FlushModeType.COMMIT); if (count != null && count >= 0) { query.setMaxResults(count); } return query.getResultList(); }