javax.persistence.criteria.Root.getModel()方法的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(11.4k)|赞(0)|评价(0)|浏览(170)

本文整理了Java中javax.persistence.criteria.Root.getModel方法的一些代码示例,展示了Root.getModel的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Root.getModel方法的具体详情如下:
包路径:javax.persistence.criteria.Root
类名称:Root
方法名:getModel

Root.getModel介绍

[英]Return the metamodel entity corresponding to the root.
[中]返回根对应的元模型实体。

代码示例

代码示例来源:origin: hibernate/hibernate-orm

/**
 * If no explicit selection was defined, we have a condition called an implicit selection if the query specified
 * a single {@link Root} and the java type of that {@link Root root's} model is the same as this criteria's
 * {@link #getResultType() result type}.
 *
 * @return True if there is an explicit selection; false otherwise.
 */
private boolean hasImplicitSelection() {
  if ( getRoots().size() != 1 ) {
    return false;
  }
  Root root = getRoots().iterator().next();
  Class<?> javaType = root.getModel().getJavaType();
  if ( javaType != null && javaType != returnType ) {
    return false;
  }
  // if we get here, the query defined no selection but defined a single root of the same type as the
  // criteria query return, so we use that as the implicit selection
  //
  // todo : should we put an implicit marker in the selection to this fact to make later processing easier?
  return true;
}

代码示例来源:origin: hibernate/hibernate-orm

@Override
protected void renderArguments(StringBuilder buffer, RenderingContext renderingContext) {
  if ( isDistinct() ) {
    buffer.append("distinct ");
  }
  else {
    // If function specifies a single non-distinct entity with ID, its alias would normally be rendered, which ends up
    // converting to the column(s) associated with the entity's ID in the rendered SQL.  However, some DBs don't support
    // the multiple columns that would end up here for entities with composite IDs.  So, since we modify the query to
    // instead specify star since that's functionally equivalent and supported by all DBs.
    List<Expression<?>> argExprs = getArgumentExpressions();
    if (argExprs.size() == 1) {
      Expression argExpr = argExprs.get(0);
      if (argExpr instanceof Root<?>) {
        Root<?> root = (Root<?>)argExpr;
        if (!root.getModel().hasSingleIdAttribute()) {
          buffer.append('*');
          return;
        }
      }
    }
  }
  super.renderArguments(buffer, renderingContext);
}

代码示例来源:origin: spring-projects/spring-data-jpa

if (root.getModel().hasSingleIdAttribute()) {
  SingularAttribute<?, ?> id = root.getModel().getId(root.getModel().getIdType().getJavaType());
  query = query.multiselect(root.get((SingularAttribute) id).alias(id.getName()));
  query = query.multiselect(root.getModel().getIdClassAttributes().stream()//
      .map(it -> (Selection<?>) root.get((SingularAttribute) it).alias(it.getName()))
      .collect(Collectors.toList()));

代码示例来源:origin: spring-projects/spring-data-jpa

/**
 * Extract the {@link Predicate} representing the {@link Example}.
 *
 * @param root must not be {@literal null}.
 * @param cb must not be {@literal null}.
 * @param example must not be {@literal null}.
 * @return never {@literal null}.
 */
public static <T> Predicate getPredicate(Root<T> root, CriteriaBuilder cb, Example<T> example) {
  Assert.notNull(root, "Root must not be null!");
  Assert.notNull(cb, "CriteriaBuilder must not be null!");
  Assert.notNull(example, "Example must not be null!");
  ExampleMatcher matcher = example.getMatcher();
  List<Predicate> predicates = getPredicates("", cb, root, root.getModel(), example.getProbe(),
      example.getProbeType(), new ExampleMatcherAccessor(matcher), new PathNode("root", null, example.getProbe()));
  if (predicates.isEmpty()) {
    return cb.isTrue(cb.literal(true));
  }
  if (predicates.size() == 1) {
    return predicates.iterator().next();
  }
  Predicate[] array = predicates.toArray(new Predicate[0]);
  return matcher.isAllMatching() ? cb.and(array) : cb.or(array);
}

代码示例来源:origin: org.omnifaces/omnipersistence

@Override
public EntityType<X> getModel() {
  return getWrapped().getModel();
}

代码示例来源:origin: org.batoo.jpa/batoo-jpa

@Override
  public String apply(Root<?> input) {
    final RootImpl<?> root = (RootImpl<?>) input;
    final StringBuilder builder = new StringBuilder(input.getModel().getName());
    if (StringUtils.isNotBlank(input.getAlias())) {
      builder.append(" as ").append(input.getAlias());
    }
    final String joins = root.generateJpqlJoins(AbstractCriteriaQueryImpl.this);
    if (StringUtils.isNotBlank(joins)) {
      builder.append("\n").append(BatooUtils.indent(joins));
    }
    return builder.toString();
  }
});

代码示例来源:origin: BatooOrg/BatooJPA

@Override
  public String apply(Root<?> input) {
    final RootImpl<?> root = (RootImpl<?>) input;
    final StringBuilder builder = new StringBuilder(input.getModel().getName());
    if (StringUtils.isNotBlank(input.getAlias())) {
      builder.append(" as ").append(input.getAlias());
    }
    final String joins = root.generateJpqlJoins(AbstractCriteriaQueryImpl.this);
    if (StringUtils.isNotBlank(joins)) {
      builder.append("\n").append(BatooUtils.indent(joins));
    }
    return builder.toString();
  }
});

代码示例来源:origin: com.haulmont.thirdparty/eclipselink

/**
 * Correlates a root of the enclosing query to a root of the subquery and
 * returns the subquery root.
 * 
 * @param parentRoot
 *            a root of the containing query
 * @return subquery root
 */
public <Y> Root<Y> correlate(Root<Y> parentRoot){
  RootImpl root = new RootImpl(parentRoot.getModel(), metamodel, parentRoot.getJavaType(), internalCorrelate((FromImpl)parentRoot), parentRoot.getModel(), (FromImpl) parentRoot);
  integrateRoot(root);
  return root;
}

代码示例来源:origin: org.seedstack.addons.jpa/jpa

@Override
  @SuppressWarnings("unchecked")
  public Predicate convert(IdentitySpecification<A, I> specification,
      JpaTranslationContext<A> context,
      SpecificationTranslator<JpaTranslationContext<A>, Predicate> translator) {
    I expectedIdentifier = specification.getExpectedIdentifier();
    Root<A> root = context.getRoot();
    return context.getCriteriaBuilder().equal(
        root.get(root.getModel().getId((Class<I>) expectedIdentifier.getClass())),
        expectedIdentifier
    );
  }
}

代码示例来源:origin: org.apache.openejb.patch/openjpa

/**
 * Correlate this subquery with the given root.
 */
public <Y> Root<Y> correlate(Root<Y> root) {
  Types.Entity<Y> entity = (Types.Entity<Y>)root.getModel();
  RootImpl<Y> corrRoot = new RootImpl<Y>(entity);
  corrRoot.setCorrelatedPath((RootImpl<Y>)root);
  _delegate.addRoot(corrRoot);
  return corrRoot;
}

代码示例来源:origin: org.apache.openjpa/openjpa-all

/**
 * Correlate this subquery with the given root.
 */
public <Y> Root<Y> correlate(Root<Y> root) {
  Types.Entity<Y> entity = (Types.Entity<Y>)root.getModel();
  RootImpl<Y> corrRoot = new RootImpl<Y>(entity);
  corrRoot.setCorrelatedPath((RootImpl<Y>)root);
  _delegate.addRoot(corrRoot);
  return corrRoot;
}

代码示例来源:origin: org.apache.openejb.patch/openjpa-persistence

/**
 * Correlate this subquery with the given root.
 */
public <Y> Root<Y> correlate(Root<Y> root) {
  Types.Entity<Y> entity = (Types.Entity<Y>)root.getModel();
  RootImpl<Y> corrRoot = new RootImpl<Y>(entity);
  corrRoot.setCorrelatedPath((RootImpl<Y>)root);
  _delegate.addRoot(corrRoot);
  return corrRoot;
}

代码示例来源:origin: org.apache.openjpa/openjpa-persistence

/**
 * Correlate this subquery with the given root.
 */
public <Y> Root<Y> correlate(Root<Y> root) {
  Types.Entity<Y> entity = (Types.Entity<Y>)root.getModel();
  RootImpl<Y> corrRoot = new RootImpl<Y>(entity);
  corrRoot.setCorrelatedPath((RootImpl<Y>)root);
  _delegate.addRoot(corrRoot);
  return corrRoot;
}

代码示例来源:origin: jaxio/generated-projects

public <T extends Identifiable<?>> Predicate byExampleOnEntity(Root<T> rootPath, 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));
  return jpaUtil.orPredicate(builder, predicates);
}

代码示例来源:origin: jaxio/jpa-query-by-example

public <T extends Identifiable<?>> Predicate byExampleOnEntity(Root<T> rootPath, 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));
  return jpaUtil.orPredicate(builder, predicates);
}

代码示例来源:origin: jaxio/jpa-query-by-example

public <T extends Identifiable<?>> Predicate byExampleOnEntity(Root<T> rootPath, T entityValue, CriteriaBuilder builder, SearchParameters sp) {
  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(byExampleOnXToMany(mt, rootPath, entityValue, sp, builder));
  return jpaUtil.concatPredicate(sp, builder, predicates);
}

代码示例来源:origin: jaxio/generated-projects

public <T extends Identifiable<?>> Predicate byExampleOnEntity(Root<T> rootPath, T entityValue, CriteriaBuilder builder, SearchParameters sp) {
  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(byExampleOnXToMany(mt, rootPath, entityValue, sp, builder));
  return jpaUtil.concatPredicate(sp, builder, predicates);
}

代码示例来源:origin: org.apache.openjpa/openjpa-all

protected void evalCrossJoinRoots(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
  Set<Root<?>> roots = q.getRoots();
  SubqueryImpl<?> subQuery = q.getDelegator();
  if (subQuery == null || subQuery.getCorrelatedJoins().isEmpty()) {
    q.assertRoot();
    if (roots.size() > 1) { // cross join
      for (Root<?> root : roots) {
        String alias = q.getAlias(root);
        Value var = factory.newBoundVariable(alias, AbstractExpressionBuilder.TYPE_OBJECT);
        var.setMetaData(((AbstractManagedType<?>)root.getModel()).meta);
        q.registerRoot(root, var);
      }
    }         
  }
}

代码示例来源:origin: org.apache.openejb.patch/openjpa-persistence

protected void evalCrossJoinRoots(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
  Set<Root<?>> roots = q.getRoots();
  SubqueryImpl<?> subQuery = q.getDelegator();
  if (subQuery == null || subQuery.getCorrelatedJoins().isEmpty()) {
    q.assertRoot();
    if (roots.size() > 1) { // cross join
      for (Root<?> root : roots) {
        String alias = q.getAlias(root);
        Value var = factory.newBoundVariable(alias, AbstractExpressionBuilder.TYPE_OBJECT);
        var.setMetaData(((AbstractManagedType<?>)root.getModel()).meta);
        q.registerRoot(root, var);
      }
    }         
  }
}

代码示例来源:origin: org.apache.openjpa/openjpa-persistence

protected void evalCrossJoinRoots(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) {
  Set<Root<?>> roots = q.getRoots();
  SubqueryImpl<?> subQuery = q.getDelegator();
  if (subQuery == null || subQuery.getCorrelatedJoins().isEmpty()) {
    q.assertRoot();
    if (roots.size() > 1) { // cross join
      for (Root<?> root : roots) {
        String alias = q.getAlias(root);
        Value var = factory.newBoundVariable(alias, AbstractExpressionBuilder.TYPE_OBJECT);
        var.setMetaData(((AbstractManagedType<?>)root.getModel()).meta);
        q.registerRoot(root, var);
      }
    }         
  }
}

相关文章