コード例 #1
0
ファイル: ByExampleUtil.java プロジェクト: radupal/app-demo
  /** Add a predicate for each simple property whose value is not null. */
  public <T> List<Predicate> byExample(
      ManagedType<T> mt,
      Path<T> mtPath,
      final T mtValue,
      SearchParameters sp,
      CriteriaBuilder builder) {
    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;
  }
コード例 #2
0
ファイル: ByExampleUtil.java プロジェクト: radupal/app-demo
 /** Construct a join predicate on collection (eg many to many, List) */
 public <T> List<Predicate> byExampleOnManyToMany(
     ManagedType<T> mt,
     Root<T> mtPath,
     final T mtValue,
     SearchParameters sp,
     CriteriaBuilder builder) {
   List<Predicate> predicates = newArrayList();
   for (PluralAttribute<T, ?, ?> pa : mt.getDeclaredPluralAttributes()) {
     if (pa.getCollectionType() == PluralAttribute.CollectionType.LIST) {
       List<?> values = (List<?>) JpaUtil.getValue(mtValue, mt.getAttribute(pa.getName()));
       if (values != null && !values.isEmpty()) {
         if (sp.getUseANDInManyToMany()) {
           if (values.size() > 3) {
             log.warn(
                 "Please note that using AND restriction on an Many to Many relationship requires as many joins as values");
           }
           for (Object value : values) {
             ListJoin<T, ?> join = mtPath.join(mt.getDeclaredList(pa.getName()));
             predicates.add(join.in(value));
           }
         } else {
           ListJoin<T, ?> join = mtPath.join(mt.getDeclaredList(pa.getName()));
           predicates.add(join.in(values));
         }
       }
     }
   }
   return predicates;
 }
コード例 #3
0
ファイル: ByExampleUtil.java プロジェクト: radupal/app-demo
 /**
  * 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 Identifiable<?>, M2O extends Identifiable<?>> List<Predicate> byExampleOnXToOne(
     ManagedType<T> mt,
     Root<T> mtPath,
     final T mtValue,
     SearchParameters sp,
     CriteriaBuilder builder) {
   List<Predicate> predicates = newArrayList();
   for (SingularAttribute<? super T, ?> attr : mt.getSingularAttributes()) {
     if (attr.getPersistentAttributeType() == MANY_TO_ONE
         || attr.getPersistentAttributeType() == ONE_TO_ONE) {
       M2O m2oValue = (M2O) JpaUtil.getValue(mtValue, mt.getAttribute(attr.getName()));
       Class<M2O> m2oType = (Class<M2O>) attr.getBindableJavaType();
       Path<M2O> m2oPath = (Path<M2O>) mtPath.get(attr);
       ManagedType<M2O> m2oMt = em.getMetamodel().entity(m2oType);
       if (m2oValue != null) {
         if (m2oValue.isIdSet()) { // we have an id, let's restrict only on this field
           predicates.add(builder.equal(m2oPath.get("id"), m2oValue.getId()));
         } else {
           predicates.addAll(byExample(m2oMt, m2oPath, m2oValue, sp, builder));
         }
       }
     }
   }
   return predicates;
 }
コード例 #4
0
ファイル: ByExampleUtil.java プロジェクト: radupal/app-demo
  public <E> Predicate byExampleOnEmbeddable(
      Path<E> embeddablePath,
      final E embeddableValue,
      SearchParameters sp,
      CriteriaBuilder builder) {
    if (embeddableValue == null) {
      return null;
    }

    Class<E> type = embeddablePath.getModel().getBindableJavaType();
    ManagedType<E> mt =
        em.getMetamodel().embeddable(type); // note: calling .managedType() does not work

    return JpaUtil.andPredicate(
        builder, byExample(mt, embeddablePath, embeddableValue, sp, builder));
  }
コード例 #5
0
ファイル: ByExampleUtil.java プロジェクト: radupal/app-demo
  public <T extends Identifiable<?>> Predicate byExampleOnEntity(
      Root<T> rootPath, final T entityValue, SearchParameters sp, CriteriaBuilder builder) {
    if (entityValue == null) {
      return null;
    }

    Class<T> type = rootPath.getModel().getBindableJavaType();
    ManagedType<T> mt = em.getMetamodel().entity(type);

    List<Predicate> predicates = newArrayList();
    predicates.addAll(byExample(mt, rootPath, entityValue, sp, builder));
    predicates.addAll(byExampleOnCompositePk(rootPath, entityValue, sp, builder));
    predicates.addAll(
        byExampleOnXToOne(mt, rootPath, entityValue, sp, builder)); // 1 level deep only
    predicates.addAll(byExampleOnManyToMany(mt, rootPath, entityValue, sp, builder));
    return JpaUtil.andPredicate(builder, predicates);
  }