List of usage examples for javax.persistence.criteria Path get
<Y> Path<Y> get(String attributeName);
From source file:net.groupbuy.dao.impl.CouponCodeDaoImpl.java
public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class); Root<CouponCode> root = criteriaQuery.from(CouponCode.class); criteriaQuery.select(root);//from www. j ava 2s. c om Predicate restrictions = criteriaBuilder.conjunction(); Path<Coupon> couponPath = root.get("coupon"); if (coupon != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date())); } } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(), criteriaBuilder.lessThan(couponPath.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("endDate").isNull(), criteriaBuilder.greaterThanOrEqualTo(couponPath.<Date>get("endDate"), new Date()))); } } if (isUsed != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:com.jaxio.jpa.querybyexample.ByPropertySelectorUtil.java
private <E> void byObjectOrModeSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates, SearchParameters sp, PropertySelector<? super E, ?> selector) { List<Predicate> selectorPredicates = newArrayList(); Path<?> path = jpaUtil.getPath(root, selector.getAttributes()); List<?> selected = selector.getSelected(); if (selected.contains(null)) { selected = newArrayList(selector.getSelected()); selected.remove(null);//from w w w. j a va2 s .c o m selectorPredicates.add(builder.isNull(path)); } if (isNotEmpty(selected)) { if (selected.get(0) instanceof Identifiable) { List<Serializable> ids = newArrayList(); for (Object selection : selected) { ids.add(((Identifiable<?>) selection).getId()); } selectorPredicates.add(path.get("id").in(ids)); } else { selectorPredicates.add(path.in(selected)); } } predicates.add(jpaUtil.orPredicate(builder, selectorPredicates)); }
From source file:com.dbs.sdwt.jpa.ByPropertySelectorUtil.java
private <E> void byObjectOrModeSelector(Root<E> root, CriteriaBuilder builder, List<Predicate> predicates, SearchParameters sp, PropertySelector<? super E, ?> selector) { List<Predicate> selectorPredicates = newArrayList(); Path<?> path = jpaUtil.getPath(root, selector.getAttributes()); List<?> selected = selector.getSelected(); if (selected.contains(null)) { selected = newArrayList(selector.getSelected()); selected.remove(null);//w w w.j a va2 s . c o m selectorPredicates.add(builder.isNull(path)); } if (isNotEmpty(selected)) { if (selected.get(0) instanceof Identifiable) { List<Serializable> ids = newArrayList(); for (Object selection : selected) { ids.add(((Identifiable<?>) selection).getId()); } if (SearchMode.NOT_IN == selector.getSearchMode()) { selectorPredicates.add(builder.not(path.get("id").in(ids))); } else { selectorPredicates.add(path.get("id").in(ids)); } } else { if (SearchMode.NOT_IN == selector.getSearchMode()) { selectorPredicates.add(builder.not(path.in(selected))); } else { selectorPredicates.add(path.in(selected)); } } } predicates.add(jpaUtil.orPredicate(builder, selectorPredicates)); }
From source file:com.dbs.sdwt.jpa.ByExampleUtil.java
public <T> List<Predicate> byExample(ManagedType<T> mt, Path<T> mtPath, T mtValue, SearchParameters sp, CriteriaBuilder builder) {//from www. jav a 2 s .com List<Predicate> predicates = newArrayList(); for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE // || attr.getPersistentAttributeType() == ONE_TO_ONE // || attr.getPersistentAttributeType() == EMBEDDED) { continue; } Object attrValue = jpaUtil.getValue(mtValue, attr); if (attrValue != null) { if (attr.getJavaType() == String.class) { if (isNotEmpty((String) attrValue)) { predicates.add(jpaUtil.stringPredicate(mtPath.get(jpaUtil.stringAttribute(mt, attr)), attrValue, sp, builder)); } } else { predicates.add(builder.equal(mtPath.get(jpaUtil.attribute(mt, attr)), attrValue)); } } } return predicates; }
From source file:de.hopmann.msc.slave.service.PackageInstallationBean.java
public PackageInstallationEntity getInstallationEntity(PackageResolved packageModel, Set<PackageInstallationEntity> requiredDependencies, PackageInstallerHolder packageInstallerHolder) { try {/* w w w. j a va 2s . co m*/ // TODO repository version, flavor, etc. CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<PackageInstallationEntity> query = cb.createQuery(PackageInstallationEntity.class); Root<PackageInstallationEntity> p = query.from(PackageInstallationEntity.class); Path<PackageInstallerEntity> pInstaller = p.get(PackageInstallationEntity_.packageInstaller); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.add(cb.equal(p.get(PackageInstallationEntity_.packageName), packageModel.getPackageName())); predicates.add(cb.equal(p.get(PackageInstallationEntity_.sourceVersion).get(Version_.versionNumber), packageModel.getPackageAccessor().getSourceVersion().getVersionNumber())); predicates.add(cb.between(pInstaller.get(PackageInstallerEntity_.version).get(Version_.versionNumber), packageInstallerHolder.getDependencyMinVersion(), packageInstallerHolder.getDependencyMaxVersion())); // TODO flavor arch if (requiredDependencies.isEmpty()) { query.where(cb.and(predicates.toArray(new Predicate[] {}))); } else { SetJoin<PackageInstallationEntity, PackageInstallationEntity> join = p .join(PackageInstallationEntity_.actualDependencies); predicates.add(join.in(requiredDependencies)); // Expression<Set<PackageInstallationEntity>> pDependencies = p // .get(PackageInstallationEntity_.actualDependencies); // predicates.add(pDependencies.in(requiredDependencies)); query.where(cb.and(predicates.toArray(new Predicate[] {}))); Path<Long> pId = p.get(PackageInstallationEntity_.id); query.groupBy(pId); query.having(cb.ge(cb.count(pId), requiredDependencies.size())); query.distinct(true); } return entityManager.createQuery(query).getSingleResult(); } catch (NoResultException e) { return null; } }
From source file:com.dbs.sdwt.jpa.JpaUtil.java
@SuppressWarnings("unchecked") public <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) { Path<?> path = root; for (Attribute<?, ?> attribute : attributes) { boolean found = false; if (path instanceof FetchParent) { for (Fetch<E, ?> fetch : ((FetchParent<?, E>) path).getFetches()) { if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) { path = (Join<E, ?>) fetch; found = true;/* w w w. j ava 2 s. co m*/ break; } } } if (!found) { if (attribute instanceof PluralAttribute) { path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT); } else { path = path.get(attribute.getName()); } } } return (Path<F>) path; }
From source file:com.yunguchang.data.ApplicationRepository.java
private TSysOrgEntity findRightFleetOfCoordinator(String coordinatorId) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<TSysOrgEntity> cq = cb.createQuery(TSysOrgEntity.class); Root<TBusBusinessRelaEntity> mapRoot = cq.from(TBusBusinessRelaEntity.class); Root<TSysUserEntity> userRoot = cq.from(TSysUserEntity.class); cq.select(mapRoot.get(TBusBusinessRelaEntity_.fleet)); Path<TSysOrgEntity> coordinatorOrg = userRoot.get(TSysUserEntity_.department); cq.where(cb.or(cb.and(cb.like(coordinatorOrg.get(TSysOrgEntity_.orgid), "001%" ), cb.equal(mapRoot.get(TBusBusinessRelaEntity_.fleet), coordinatorOrg)), cb.and(cb.notLike(coordinatorOrg.get(TSysOrgEntity_.orgid), "001%"), cb.equal(mapRoot.get(TBusBusinessRelaEntity_.busOrg), coordinatorOrg)) ),// ww w .j a va2s. c o m cb.equal(userRoot.get(TSysUserEntity_.userid), coordinatorId) ); return Iterables.getFirst(em.createQuery(cq).getResultList(), null); }
From source file:net.sf.gazpachoquest.qbe.ByExampleSpecification.java
public <T extends Persistable> Specification<T> byExampleOnEntity(final T example, final SearchParameters sp) { Validate.notNull(example, "example must not be null"); return new Specification<T>() { @Override/*from ww w .j a v a 2 s. co m*/ public Predicate toPredicate(final Root<T> rootPath, final CriteriaQuery<?> query, final CriteriaBuilder builder) { Class<T> type = rootPath.getModel().getBindableJavaType(); ManagedType<T> mt = em.getMetamodel().entity(type); List<Predicate> predicates = new ArrayList<Predicate>(); predicates.addAll(byExample(mt, rootPath, example, sp, builder)); predicates.addAll(byExampleOnXToOne(mt, rootPath, example, sp, builder)); // 1 level deep only predicates.addAll(byExampleOnManyToMany(mt, rootPath, example, sp, builder)); // order by query.orderBy(JpaUtil.buildJpaOrders(sp.getOrders(), rootPath, builder)); return JpaUtil.andPredicate(builder, predicates); } //https://github.com/jaxio/generated-projects/tree/master/jsf2-spring-conversation/src/main/generated-java/com/jaxio/appli/repository/support public <T extends Persistable> List<Predicate> byExample(final ManagedType<T> mt, final Path<T> mtPath, final T mtValue, final SearchParameters sp, final CriteriaBuilder builder) { List<Predicate> predicates = new ArrayList<Predicate>(); for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE || attr.getPersistentAttributeType() == EMBEDDED || attr.getJavaType().isAssignableFrom(Map.class)) { continue; } Object attrValue = getValue(mtValue, attr); if (attrValue != null) { if (attr.getJavaType() == String.class) { if (isNotEmpty((String) attrValue)) { SingularAttribute<? super T, String> stringAttribute = mt .getSingularAttribute(attr.getName(), String.class); predicates.add(JpaUtil.stringPredicate(mtPath.get(stringAttribute), attrValue, sp, builder)); } } else { SingularAttribute<? super T, ?> attribute = mt.getSingularAttribute(attr.getName(), attr.getJavaType()); // apply equal predicates.add(builder.equal(mtPath.get(attribute), attrValue)); } } } return predicates; } /** * Construct a join predicate on collection (eg many to many, List) */ public <T extends Persistable> List<Predicate> byExampleOnManyToMany(final ManagedType<T> mt, final Root<T> mtPath, final T mtValue, final SearchParameters sp, final CriteriaBuilder builder) { List<Predicate> predicates = new ArrayList<Predicate>(); for (PluralAttribute<T, ?, ?> pa : mt.getDeclaredPluralAttributes()) { if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) { List<?> value = (List<?>) getValue(mtValue, mt.getAttribute(pa.getName())); if (value != null && !value.isEmpty()) { ListJoin<T, ?> join = mtPath.join(mt.getDeclaredList(pa.getName())); predicates.add(join.in(value)); } } if (pa.getCollectionType() == PluralAttribute.CollectionType.SET) { Set<?> value = (Set<?>) getValue(mtValue, mt.getAttribute(pa.getName())); if (value != null && !value.isEmpty()) { SetJoin<T, ?> join = mtPath.join(mt.getDeclaredSet(pa.getName())); predicates.add(join.in(value)); } } } return predicates; } /** * Invoke byExample method for each not null x-to-one association * when their pk is not set. This allows you * to search entities based on an associated entity's properties * value. */ @SuppressWarnings("unchecked") public <T extends Persistable, M2O extends Persistable> List<Predicate> byExampleOnXToOne( final ManagedType<T> mt, final Root<T> mtPath, final T mtValue, final SearchParameters sp, final CriteriaBuilder builder) { List<Predicate> predicates = new ArrayList<Predicate>(); for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) { // M2O m2oValue = (M2O) getValue(mtValue, mt.getAttribute(attr.getName())); if (m2oValue != null) { Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType(); ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType); Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr); predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder)); } } } return predicates; } private <T> Object getValue(final T example, final Attribute<? super T, ?> attr) { Object value = null; try { if (attr.getJavaMember() instanceof Method) { value = ((Method) attr.getJavaMember()).invoke(example, new Object[0]); } else if (attr.getJavaMember() instanceof Field) { value = ReflectionUtils.getField((Field) attr.getJavaMember(), example); } if (value instanceof ValueHolderInterface) { value = ((ValueHolderInterface) value).getValue(); } } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw new RuntimeException(e); } return value; } }; }
From source file:fi.vm.sade.eperusteet.ylops.service.ops.impl.OpetussuunnitelmaServiceImpl.java
private CriteriaQuery<Opetussuunnitelma> getQuery(OpetussuunnitelmaQuery pquery) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Opetussuunnitelma> query = builder.createQuery(Opetussuunnitelma.class); Root<Opetussuunnitelma> ops = query.from(Opetussuunnitelma.class); List<Predicate> ehdot = new ArrayList<>(); // VAIN JULKAISTUT ehdot.add(builder.equal(ops.get(Opetussuunnitelma_.tila), Tila.JULKAISTU)); // Haettu organisaatio lytyy opsilta if (pquery.getOrganisaatio() != null) { Expression<Set<String>> organisaatiot = ops.get(Opetussuunnitelma_.organisaatiot); ehdot.add(builder.and(builder.isMember(pquery.getOrganisaatio(), organisaatiot))); }/*from www . j a v a2s .c o m*/ // Koulutustyyppi if (pquery.getKoulutustyyppi() != null) { ehdot.add(builder.and(builder.equal(ops.get(Opetussuunnitelma_.koulutustyyppi), KoulutusTyyppi.of(pquery.getKoulutustyyppi())))); } // Perusteen tyyppi if (pquery.getTyyppi() != null) { ehdot.add(builder.and(builder.equal(ops.get(Opetussuunnitelma_.tyyppi), pquery.getTyyppi()))); } // Perusteen id if (pquery.getPerusteenId() != null) { Path<PerusteCache> cachedPeruste = ops.join(Opetussuunnitelma_.cachedPeruste); ehdot.add(builder .and(builder.equal(cachedPeruste.get(PerusteCache_.perusteId), pquery.getPerusteenId()))); } // Perusteen diaarinumero if (pquery.getPerusteenDiaarinumero() != null) { ehdot.add(builder.and(builder.equal(ops.get(Opetussuunnitelma_.perusteenDiaarinumero), pquery.getPerusteenDiaarinumero()))); } query.where(ehdot.toArray(new Predicate[ehdot.size()])); return query.select(ops); }
From source file:ca.uhn.fhir.jpa.dao.SearchBuilder.java
private List<Predicate> createPredicateTagList(Path<TagDefinition> theDefJoin, CriteriaBuilder theBuilder, TagTypeEnum theTagType, List<Pair<String, String>> theTokens) { Predicate typePrediate = theBuilder.equal(theDefJoin.get("myTagType"), theTagType); List<Predicate> orPredicates = Lists.newArrayList(); for (Pair<String, String> next : theTokens) { Predicate codePrediate = theBuilder.equal(theDefJoin.get("myCode"), next.getRight()); if (isNotBlank(next.getLeft())) { Predicate systemPrediate = theBuilder.equal(theDefJoin.get("mySystem"), next.getLeft()); orPredicates.add(theBuilder.and(typePrediate, systemPrediate, codePrediate)); } else {//from w w w . ja v a2 s .co m orPredicates.add(theBuilder.and(typePrediate, codePrediate)); } } return orPredicates; }