@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); }
Join<TaskImpl,PeopleAssignmentsImpl> peopleAssignJoin = null; if( taskRoot != null ) { for( Join<TaskImpl,?> join : taskRoot.getJoins() ) { if( join.getJavaType().equals(PeopleAssignmentsImpl.class) ) { peopleAssignJoin = (Join<TaskImpl, PeopleAssignmentsImpl>) join;
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); } }
renderJoins( jpaqlQuery, renderingContext, root.getJoins() ); if ( root instanceof RootImpl ) { Set<TreatedRoot> treats = ( (RootImpl) root ).getTreats();
@Override public Set<Join<X, ?>> getJoins() { return getWrapped().getJoins(); }
protected void visitOnExpressions(XAbstractQuery<?> abstractQuery) { for (Root<?> root : abstractQuery.getRoots()) { for (Join<?, ?> join : root.getJoins()) { this.visitOnExpressions(join); } } }
@Override public long count(Specification<A> specification) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); Class<A> entityClass = getAggregateRootClass(); CriteriaQuery<Long> cq = cb.createQuery(Long.class); Root<A> root = cq.from(entityClass); cq.select(cb.count(root)); cq.where( specificationTranslator.translate(specification, new JpaTranslationContext<>(cb, root))); if (!root.getJoins().isEmpty()) { // When we have joins, we need to deduplicate the results cq.distinct(true); } return entityManager.createQuery(cq).getSingleResult(); }
private void renderRoots(StringBuilder buffer, Collection<Root<?>> roots) { if (roots == null) return; int i = 0; for (Root r : roots) { buffer.append(((ExpressionImpl<?>)r).asVariable(this)); if (++i != roots.size()) buffer.append(", "); renderJoins(buffer, r.getJoins()); renderFetches(buffer, r.getFetches()); } } private void renderFetches(StringBuilder buffer, Set<Fetch> fetches) {
private void renderRoots(StringBuilder buffer, Collection<Root<?>> roots) { if (roots == null) return; int i = 0; for (Root r : roots) { buffer.append(((ExpressionImpl<?>)r).asVariable(this)); if (++i != roots.size()) buffer.append(", "); renderJoins(buffer, r.getJoins()); renderFetches(buffer, r.getFetches()); } } private void renderFetches(StringBuilder buffer, Set<Fetch> fetches) {
private void renderRoots(StringBuilder buffer, Collection<Root<?>> roots) { if (roots == null) return; int i = 0; for (Root r : roots) { buffer.append(((ExpressionImpl<?>)r).asVariable(this)); if (++i != roots.size()) buffer.append(", "); renderJoins(buffer, r.getJoins()); renderFetches(buffer, r.getFetches()); } } private void renderFetches(StringBuilder buffer, Set<Fetch> fetches) {
private void renderRoots(StringBuilder buffer, Collection<Root<?>> roots) { if (roots == null) return; int i = 0; for (Root r : roots) { buffer.append(((ExpressionImpl<?>)r).asVariable(this)); if (++i != roots.size()) buffer.append(", "); renderJoins(buffer, r.getJoins()); renderFetches(buffer, r.getFetches()); } } private void renderFetches(StringBuilder buffer, Set<Fetch> fetches) {
@SuppressWarnings("unchecked") protected <T> SetJoin<GroupEntity, T> createOrGetJoin(String alias, Root<GroupEntity> root, SetAttribute<GroupEntity, T> attribute) { for (Join<GroupEntity, ?> currentJoin : root.getJoins()) { if (currentJoin.getAlias().equals(alias)) { return (SetJoin<GroupEntity, T>) currentJoin; } } final SetJoin<GroupEntity, T> join = root.join(attribute, JoinType.LEFT); join.alias(alias); return join; }
@SuppressWarnings("unchecked") protected Join<UserEntity, NameEntity> createOrGetJoinForName(final Root<UserEntity> root) { for (final Join<UserEntity, ?> currentJoin : root.getJoins()) { if (currentJoin.getAlias() != null && currentJoin.getAlias().equals(JOIN_ALIAS_FOR_NAME)) { return (Join<UserEntity, NameEntity>) currentJoin; } } Join<UserEntity, NameEntity> join = root.join(UserEntity_.name, JoinType.LEFT); join.alias(JOIN_ALIAS_FOR_NAME); return join; }
public static boolean hasManyRootsFetchesOrJoins(CriteriaQuery<?> criteriaQuery) { Set<Root<?>> roots = criteriaQuery.getRoots(); // more than one root, user is supposed to handle this manually if (roots.size() != 1) return false; for (Root<?> root : roots) { if (containsMultiRelationFetch(root.getFetches())) return true; if (containsMultiRelationJoin(root.getJoins())) return true; } return false; }
@Override public Stream<A> get(Specification<A> specification, Option... options) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); Class<A> entityClass = getAggregateRootClass(); CriteriaQuery<A> cq = cb.createQuery(entityClass); Root<A> root = cq.from(entityClass); cq.where( specificationTranslator.translate(specification, new JpaTranslationContext<>(cb, root))); if (!root.getJoins().isEmpty()) { // When we have joins, we need to deduplicate the results cq.distinct(true); } for (Option option : options) { if (option instanceof SortOption) { applySort(cb, root, cq, (SortOption) option); } } return buildStream(applyOffsetAndLimit(entityManager.createQuery(cq), options)); }
@SuppressWarnings("unchecked") protected SetJoin<UserEntity, ExtensionFieldValueEntity> createOrGetJoin(String alias, Root<UserEntity> root, SetAttribute<UserEntity, ExtensionFieldValueEntity> attribute) { for (Join<UserEntity, ?> currentJoin : root.getJoins()) { if (currentJoin.getAlias() == null) { // if alias is null, it is not an alias for an extension join, so we ignore it continue; } if (currentJoin.getAlias().equals(alias)) { return (SetJoin<UserEntity, ExtensionFieldValueEntity>) currentJoin; } } final SetJoin<UserEntity, ExtensionFieldValueEntity> join = root.join(attribute, JoinType.LEFT); join.alias(alias); return join; } }
protected void evalAccessPaths(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { Set<ClassMetaData> metas = new HashSet<ClassMetaData>(); MetamodelImpl metamodel = q.getMetamodel(); for (Root<?> root : q.getRoots()) { metas.add(((AbstractManagedType<?>)root.getModel()).meta); for (Join<?,?> join : root.getJoins()) { Class<?> cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.getRepository().getMetaData(cls, null, true); PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } } // TODO -- need to handle subqueries exps.accessPath = metas.toArray(new ClassMetaData[metas.size()]); }
protected void evalAccessPaths(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { Set<ClassMetaData> metas = new HashSet<ClassMetaData>(); MetamodelImpl metamodel = q.getMetamodel(); for (Root<?> root : q.getRoots()) { metas.add(((AbstractManagedType<?>)root.getModel()).meta); for (Join<?,?> join : root.getJoins()) { Class<?> cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.getRepository().getMetaData(cls, null, true); PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } } // TODO -- need to handle subqueries exps.accessPath = metas.toArray(new ClassMetaData[metas.size()]); }
protected void evalAccessPaths(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { Set<ClassMetaData> metas = new HashSet<ClassMetaData>(); MetamodelImpl metamodel = q.getMetamodel(); for (Root<?> root : q.getRoots()) { metas.add(((AbstractManagedType<?>)root.getModel()).meta); for (Join<?,?> join : root.getJoins()) { Class<?> cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.getRepository().getMetaData(cls, null, true); PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } for (Fetch<?,?> fetch : root.getFetches()) { metas.add(metamodel.getRepository().getCachedMetaData(fetch.getAttribute().getJavaType())); } } exps.accessPath = metas.toArray(new ClassMetaData[metas.size()]); }
protected void evalAccessPaths(QueryExpressions exps, ExpressionFactory factory, CriteriaQueryImpl<?> q) { Set<ClassMetaData> metas = new HashSet<ClassMetaData>(); MetamodelImpl metamodel = q.getMetamodel(); for (Root<?> root : q.getRoots()) { metas.add(((AbstractManagedType<?>)root.getModel()).meta); for (Join<?,?> join : root.getJoins()) { Class<?> cls = join.getAttribute().getJavaType(); if (join.getAttribute().isAssociation()) { ClassMetaData meta = metamodel.getRepository().getMetaData(cls, null, true); PersistenceType type = MetamodelImpl.getPersistenceType(meta); if (type == PersistenceType.ENTITY || type == PersistenceType.EMBEDDABLE) metas.add(meta); } } for (Fetch<?,?> fetch : root.getFetches()) { metas.add(metamodel.getRepository().getCachedMetaData(fetch.getAttribute().getJavaType())); } } exps.accessPath = metas.toArray(new ClassMetaData[metas.size()]); }