/** * 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; }
@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); }
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()));
/** * 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); }
@Override public EntityType<X> getModel() { return getWrapped().getModel(); }
@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(); } });
@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(); } });
/** * 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; }
@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 ); } }
/** * 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; }
/** * 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; }
/** * 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; }
/** * 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; }
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); }
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); }
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); }
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); }
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); } } } }
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); } } } }
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); } } } }