/** * This is a helper method to retrieve a particular {@link Root} from a {@link CriteriaQuery} instance * * @param query The {@link CriteriaQuery} instance that we're building * @param queryType The {@link Class} matching the {@link Root} we want * @return The {@link Root} matching the given {@link Class} or null if it's not in the query */ public static <T> Root getRoot(AbstractQuery<T> query, Class queryType) { Root<?> table = null; for( Root<?> root : query.getRoots() ) { if( root.getJavaType().equals(queryType) ) { table = root; break; } } return table; }
} else { throw new IllegalStateException("List id [" + getQueryParameterIdNameMap().get(Integer.parseInt(criteria.getListId())) + "] is not supported for queries on " + table.getJavaType().getSimpleName() + ".");
@Override protected <T> Expression getEntityField(CriteriaQuery<T> query, String listId, Attribute attr) { if( attr == null ) { return null; } Root<TaskImpl> taskRoot = null; Join<TaskImpl, TaskDataImpl> taskDataJoin = null; Join<TaskImpl, PeopleAssignmentsImpl> peopAssignJoin = null; for( Root root : query.getRoots() ) { if( TaskImpl.class.equals(root.getJavaType()) ) { taskRoot = (Root<TaskImpl>) root; for( Join<TaskImpl, ?> join : taskRoot.getJoins() ) { if( TaskDataImpl.class.equals(join.getJavaType()) ) { taskDataJoin = (Join<TaskImpl, TaskDataImpl>) join; } else if( PeopleAssignmentsImpl.class.equals(join.getJavaType()) ) { peopAssignJoin = (Join<TaskImpl, PeopleAssignmentsImpl>) join; } } } } assert taskRoot != null : "Unable to find TaskImpl Root in query!"; if( taskDataJoin == null ) { taskDataJoin = taskRoot.join(TaskImpl_.taskData); } assert taskDataJoin != null : "Unable to find TaskDataImpl Join in query!"; return taskImplSpecificGetEntityField(query, taskRoot, taskDataJoin, peopAssignJoin, listId, attr); }
private <T> void useDistinctWhenLefOuterJoinsPresent(CriteriaQuery<T> criteriaQuery) { boolean useDistinct = false; Root<TaskImpl> taskRoot = null; ROOTS_FOR: for( Root root : criteriaQuery.getRoots() ) { if( TaskImpl.class.equals(root.getJavaType()) ) { taskRoot = (Root<TaskImpl>) root; for( Join<TaskImpl, ?> taskJoin : taskRoot.getJoins() ) { if( PeopleAssignmentsImpl.class.equals(taskJoin.getJavaType()) ) { Join<TaskImpl, PeopleAssignmentsImpl> peopleAssignJoin = (Join<TaskImpl, PeopleAssignmentsImpl>) taskJoin; if( JoinType.LEFT.equals(peopleAssignJoin.getJoinType()) ) { useDistinct = true; break ROOTS_FOR; } for( Join peopleAssignJoinJoin : peopleAssignJoin.getJoins() ) { if( JoinType.LEFT.equals(peopleAssignJoinJoin.getJoinType()) ) { useDistinct = true; break ROOTS_FOR; } } } } } } if( useDistinct ) { criteriaQuery.distinct(true); } }
Class entityClazz = from.getJavaType();
@Override public Class<? extends X> getJavaType() { return getWrapped().getJavaType(); }
private static <T extends WithId> Root<T> getMainSelection(CriteriaQuery<T> query) { return query.getRoots().stream() .filter(r -> r.getJavaType().isAssignableFrom(query.getResultType())) .map(r -> (Root<T>) r) .filter(r -> !r.isCorrelated()) .findFirst() .orElseThrow(IllegalStateException::new); }
private void join(Root<Object> root, List<String> joinFetchTables, JoinType type) { if (joinFetchTables != null && (joinFetchTables.size() > 0)) { logger.debug("{} join root {} with table {}", type.name(), root.getJavaType(), joinFetchTables); for (String table : joinFetchTables) { if (table != null) root.fetch(table, type); } } }
/** * Return the ORM path from the given rule. */ private <T> Path<T> getOrmPath(final Root<U> root, final BasicRule rule) { final String path = mapping.getOrDefault(rule.getField(), mapping.containsKey("*") ? rule.getField() : null); if (path == null) { // Invalid path, coding issue or SQL injection attempt log.error(String.format("Non mapped property '%s' found for entity class '%s'", rule.getField(), root.getJavaType().getName())); return null; } return getOrmPath(root, path); }
/** * Find the Root with type class on CriteriaQuery Root Set * * @param <T> root type * @param query criteria query * @param clazz root type * @return Root<T> of null if none */ public static <T> Root<T> findRoot(CriteriaQuery<?> query, Class<T> clazz) { for (Root<?> r : query.getRoots()) { if (clazz.equals(r.getJavaType())) { return (Root<T>) r.as(clazz); } } return null; }
private <T> Predicate onSimplePrimaryKey(Root<T> root, CriteriaBuilder builder, SearchParameters sp, List<SingularAttribute<?, ?>> properties) { List<Serializable> ids = hibernateSearchUtil.findId(root.getJavaType(), sp, properties); if (ids == null) { return null; } else if (ids.isEmpty()) { return builder.disjunction(); } return jpaUtil.concatPredicate(sp, builder, root.get("id").in(ids)); }
private <T> Predicate onSimplePrimaryKey(Root<T> root, CriteriaBuilder builder, SearchParameters sp, List<SingularAttribute<?, ?>> properties) { List<Serializable> ids = hibernateSearchUtil.findId(root.getJavaType(), sp, properties); if (ids == null) { return null; } else if (ids.isEmpty()) { return builder.disjunction(); } return jpaUtil.concatPredicate(sp, builder, root.get("id").in(ids)); }
private <T extends Identifiable<?>> Predicate onCompositePrimaryKeys(Root<T> root, CriteriaBuilder builder, SearchParameters sp, List<SingularAttribute<?, ?>> properties) { List<? extends T> found = hibernateSearchUtil.find(root.getJavaType(), sp, properties); if (found == null) { return null; } else if (found.isEmpty()) { return builder.disjunction(); } List<Predicate> predicates = newArrayList(); for (T t : found) { predicates.add(byExampleOnEntity(root, t, sp, builder)); } return jpaUtil.concatPredicate(sp, builder, jpaUtil.orPredicate(builder, predicates)); }
private <T extends Identifiable<?>> Predicate onCompositePrimaryKeys(Root<T> root, CriteriaBuilder builder, SearchParameters sp, List<SingularAttribute<?, ?>> properties) { List<? extends T> found = hibernateSearchUtil.find(root.getJavaType(), sp, properties); if (found == null) { return null; } else if (found.isEmpty()) { return builder.disjunction(); } List<Predicate> predicates = newArrayList(); for (T t : found) { predicates.add(byExampleOnEntity(root, t, sp, builder)); } return jpaUtil.concatPredicate(sp, builder, jpaUtil.orPredicate(builder, predicates)); }
public void populate(Object parsed, ExpressionStoreQuery query) { CriteriaQueryImpl<?> c = (CriteriaQueryImpl<?>) parsed; query.invalidateCompilation(); query.getContext().setCandidateType(c.getRoot().getJavaType(), true); query.setQuery(parsed); }
public void populate(Object parsed, ExpressionStoreQuery query) { CriteriaQueryImpl<?> c = (CriteriaQueryImpl<?>) parsed; query.invalidateCompilation(); query.getContext().setCandidateType(c.getRoot().getJavaType(), true); query.setQuery(parsed); }
public void populate(Object parsed, ExpressionStoreQuery query) { CriteriaQueryImpl<?> c = (CriteriaQueryImpl<?>) parsed; query.invalidateCompilation(); query.getContext().setCandidateType(c.getRoot().getJavaType(), true); query.setQuery(parsed); }
public void populate(Object parsed, ExpressionStoreQuery query) { CriteriaQueryImpl<?> c = (CriteriaQueryImpl<?>) parsed; query.invalidateCompilation(); query.getContext().setCandidateType(c.getRoot().getJavaType(), true); query.setQuery(parsed); }
/** * 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; }
/** * Copy Criteria without Selection * * @param from source Criteria * @param to destination Criteria */ public static void copyCriteriaNoSelection(CriteriaQuery<?> from, CriteriaQuery<?> to) { // Copy Roots for (Root<?> root : from.getRoots()) { Root<?> dest = to.from(root.getJavaType()); dest.alias(getOrCreateAlias(root)); copyJoins(root, dest); } if (from.getGroupList() != null) to.groupBy(from.getGroupList()); to.distinct(from.isDistinct()); if (from.getGroupRestriction() != null) to.having(from.getGroupRestriction()); if (from.getRestriction() != null) to.where(from.getRestriction()); if (from.getOrderList() != null) to.orderBy(from.getOrderList()); }