javax.persistence.criteria.ListJoin Java Examples

The following examples show how to use javax.persistence.criteria.ListJoin. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JpaDeploymentManagement.java    From hawkbit with Eclipse Public License 1.0 8 votes vote down vote up
@Override
public Page<String> findMessagesByActionStatusId(final Pageable pageable, final long actionStatusId) {
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    final CriteriaQuery<Long> countMsgQuery = cb.createQuery(Long.class);
    final Root<JpaActionStatus> countMsgQueryFrom = countMsgQuery.distinct(true).from(JpaActionStatus.class);
    final ListJoin<JpaActionStatus, String> cJoin = countMsgQueryFrom.joinList("messages", JoinType.LEFT);
    countMsgQuery.select(cb.count(cJoin))
            .where(cb.equal(countMsgQueryFrom.get(JpaActionStatus_.id), actionStatusId));
    final Long totalCount = entityManager.createQuery(countMsgQuery).getSingleResult();

    final CriteriaQuery<String> msgQuery = cb.createQuery(String.class);
    final Root<JpaActionStatus> as = msgQuery.from(JpaActionStatus.class);
    final ListJoin<JpaActionStatus, String> join = as.joinList("messages", JoinType.LEFT);
    final CriteriaQuery<String> selMsgQuery = msgQuery.select(join);
    selMsgQuery.where(cb.equal(as.get(JpaActionStatus_.id), actionStatusId));

    final List<String> result = new ArrayList<>(entityManager.createQuery(selMsgQuery)
            .setFirstResult((int) pageable.getOffset()).setMaxResults(pageable.getPageSize()).getResultList());

    return new PageImpl<>(result, pageable, totalCount);
}
 
Example #2
Source File: UserSpecifications.java    From spring-microservice-sample with GNU General Public License v3.0 6 votes vote down vote up
public static Specification<User> byKeyword(String keyword, String role, String active){
    return (Root<User> root, CriteriaQuery<?> query, CriteriaBuilder cb) -> {

        List<Predicate> predicates = new ArrayList<>();

        if (StringUtils.hasText(keyword)) {
            predicates.add(
                cb.or(
                    cb.like(root.get(User_.email), "%" + keyword + "%"),
                    cb.like(root.get(User_.username), "%" + keyword + "%")
                ));
        }

        if (StringUtils.hasText(role) && !"ALL".equals(role)) {

            ListJoin<User, String> roleJoin = root.join(User_.roles);
            predicates.add(cb.equal(roleJoin, role));
        }
        if (StringUtils.hasText(active)) {
            predicates.add(cb.equal(root.get(User_.active), Boolean.valueOf(active)));
        }
        return cb.and(predicates.toArray(new Predicate[predicates.size()]));
    };
}
 
Example #3
Source File: JpaRolloutGroupManagement.java    From hawkbit with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Page<Target> findTargetsOfRolloutGroupByRsql(final Pageable pageable, final long rolloutGroupId,
        final String rsqlParam) {

    throwExceptionIfRolloutGroupDoesNotExist(rolloutGroupId);

    final Specification<JpaTarget> rsqlSpecification = RSQLUtility.parse(rsqlParam, TargetFields.class,
            virtualPropertyReplacer, database);

    return convertTPage(targetRepository.findAll((root, query, criteriaBuilder) -> {
        final ListJoin<JpaTarget, RolloutTargetGroup> rolloutTargetJoin = root.join(JpaTarget_.rolloutTargetGroup);
        return criteriaBuilder.and(rsqlSpecification.toPredicate(root, query, criteriaBuilder),
                criteriaBuilder.equal(
                        rolloutTargetJoin.get(RolloutTargetGroup_.rolloutGroup).get(JpaRolloutGroup_.id),
                        rolloutGroupId));
    }, pageable), pageable);
}
 
Example #4
Source File: TargetSpecifications.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@link Specification} for retrieving {@link Target}s that don't have the
 * given distribution set in their action history
 *
 * @param distributionSetId
 *            the ID of the distribution set which must not be assigned
 * @return the {@link Target} {@link Specification}
 */
public static Specification<JpaTarget> hasNotDistributionSetInActions(final Long distributionSetId) {
    return (targetRoot, query, cb) -> {
        final ListJoin<JpaTarget, JpaAction> actionsJoin = targetRoot.join(JpaTarget_.actions, JoinType.LEFT);
        actionsJoin.on(cb.equal(actionsJoin.get(JpaAction_.distributionSet).get(JpaDistributionSet_.id),
                distributionSetId));

        return cb.isNull(actionsJoin.get(JpaAction_.id));
    };
}
 
Example #5
Source File: JpaRolloutGroupManagement.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public Page<TargetWithActionStatus> findAllTargetsOfRolloutGroupWithActionStatus(final Pageable pageRequest,
        final long rolloutGroupId) {
    throwExceptionIfRolloutGroupDoesNotExist(rolloutGroupId);

    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    final CriteriaQuery<Object[]> query = cb.createQuery(Object[].class);
    final CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);

    final Root<RolloutTargetGroup> targetRoot = query.distinct(true).from(RolloutTargetGroup.class);
    final Join<RolloutTargetGroup, JpaTarget> targetJoin = targetRoot.join(RolloutTargetGroup_.target);
    final ListJoin<RolloutTargetGroup, JpaAction> actionJoin = targetRoot.join(RolloutTargetGroup_.actions,
            JoinType.LEFT);

    final Root<RolloutTargetGroup> countQueryFrom = countQuery.distinct(true).from(RolloutTargetGroup.class);
    countQueryFrom.join(RolloutTargetGroup_.target);
    countQueryFrom.join(RolloutTargetGroup_.actions, JoinType.LEFT);
    countQuery.select(cb.count(countQueryFrom)).where(cb
            .equal(countQueryFrom.get(RolloutTargetGroup_.rolloutGroup).get(JpaRolloutGroup_.id), rolloutGroupId));
    final Long totalCount = entityManager.createQuery(countQuery).getSingleResult();

    final CriteriaQuery<Object[]> multiselect = query.multiselect(targetJoin, actionJoin.get(JpaAction_.status))
            .where(cb.equal(targetRoot.get(RolloutTargetGroup_.rolloutGroup).get(JpaRolloutGroup_.id),
                    rolloutGroupId));
    final List<TargetWithActionStatus> targetWithActionStatus = entityManager.createQuery(multiselect)
            .setFirstResult((int) pageRequest.getOffset()).setMaxResults(pageRequest.getPageSize()).getResultList()
            .stream().map(o -> new TargetWithActionStatus((Target) o[0], (Action.Status) o[1]))
            .collect(Collectors.toList());

    return new PageImpl<>(targetWithActionStatus, pageRequest, totalCount);
}
 
Example #6
Source File: TargetSpecifications.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@link Specification} for retrieving {@link Target}s that have no Action
 * of the {@link RolloutGroup}.
 *
 * @param group
 *            the {@link RolloutGroup}
 * @return the {@link Target} {@link Specification}
 */
public static Specification<JpaTarget> hasNoActionInRolloutGroup(final Long group) {
    return (targetRoot, query, cb) -> {
        final ListJoin<JpaTarget, RolloutTargetGroup> rolloutTargetJoin = targetRoot
                .join(JpaTarget_.rolloutTargetGroup, JoinType.INNER);
        rolloutTargetJoin.on(
                cb.equal(rolloutTargetJoin.get(RolloutTargetGroup_.rolloutGroup).get(JpaRolloutGroup_.id), group));

        final ListJoin<JpaTarget, JpaAction> actionsJoin = targetRoot.join(JpaTarget_.actions, JoinType.LEFT);
        actionsJoin.on(cb.equal(actionsJoin.get(JpaAction_.rolloutGroup).get(JpaRolloutGroup_.id), group));

        return cb.isNull(actionsJoin.get(JpaAction_.id));
    };
}
 
Example #7
Source File: TargetSpecifications.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@link Specification} for retrieving {@link Target}s that are not in the
 * given {@link RolloutGroup}s
 *
 * @param groups
 *            the {@link RolloutGroup}s
 * @return the {@link Target} {@link Specification}
 */
public static Specification<JpaTarget> isNotInRolloutGroups(final Collection<Long> groups) {
    return (targetRoot, query, cb) -> {
        final ListJoin<JpaTarget, RolloutTargetGroup> rolloutTargetJoin = targetRoot
                .join(JpaTarget_.rolloutTargetGroup, JoinType.LEFT);
        final Predicate inRolloutGroups = rolloutTargetJoin.get(RolloutTargetGroup_.rolloutGroup)
                .get(JpaRolloutGroup_.id).in(groups);
        rolloutTargetJoin.on(inRolloutGroups);
        return cb.isNull(rolloutTargetJoin.get(RolloutTargetGroup_.target));
    };
}
 
Example #8
Source File: TagRepositoryHibernate.java    From realworld-api-quarkus with MIT License 5 votes vote down vote up
@Override
public List<Tag> findArticleTags(Long articleId) {
  CriteriaBuilder builder = getCriteriaBuilder();
  CriteriaQuery<Tag> criteriaQuery = getCriteriaQuery(builder);
  Root<Tag> tags = getRoot(criteriaQuery);
  ListJoin<Tag, ArticlesTags> articlesTags = tags.joinList("articlesTags");
  criteriaQuery.select(tags);
  criteriaQuery.where(builder.equal(articlesTags.get("article").get("id"), articleId));
  return getResultList(criteriaQuery);
}
 
Example #9
Source File: DistributionSetSpecification.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param assignedTargetId
 *            the targetID which is assigned to a distribution set to search
 *            for.
 * @return the specification to search for a distribution set which is
 *         assigned to the given targetId
 */
public static Specification<JpaDistributionSet> assignedTarget(final String assignedTargetId) {
    return (dsRoot, query, cb) -> {
        final ListJoin<JpaDistributionSet, JpaTarget> assignedTargetJoin = dsRoot
                .join(JpaDistributionSet_.assignedToTargets, JoinType.INNER);
        return cb.equal(assignedTargetJoin.get(JpaTarget_.controllerId), assignedTargetId);
    };
}
 
Example #10
Source File: DistributionSetSpecification.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @param installedTargetId
 *            the targetID which is installed to a distribution set to
 *            search for.
 * @return the specification to search for a distribution set which is
 *         installed to the given targetId
 */
public static Specification<JpaDistributionSet> installedTarget(final String installedTargetId) {
    return (dsRoot, query, cb) -> {
        final ListJoin<JpaDistributionSet, JpaTarget> installedTargetJoin = dsRoot
                .join(JpaDistributionSet_.installedAtTargets, JoinType.INNER);
        return cb.equal(installedTargetJoin.get(JpaTarget_.controllerId), installedTargetId);
    };
}
 
Example #11
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"unchecked"})
public <X, Y> ListJoin<X, Y> joinList(String attributeName, JoinType jt) {
	final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute( attributeName );
	if ( !attribute.isCollection() ) {
		throw new IllegalArgumentException( "Requested attribute was not a list" );
	}

	final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
	if ( !PluralAttribute.CollectionType.LIST.equals( pluralAttribute.getCollectionType() ) ) {
		throw new IllegalArgumentException( "Requested attribute was not a list" );
	}

	return (ListJoin<X, Y>) join( (ListAttribute) attribute, jt );
}
 
Example #12
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <X, Y> ListJoin<X, Y> joinList(String attributeName) {
	return joinList( attributeName, DEFAULT_JOIN_TYPE );
}
 
Example #13
Source File: AbstractFromImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <Y> ListJoin<X, Y> join(ListAttribute<? super X, Y> list) {
	return join( list, DEFAULT_JOIN_TYPE );
}
 
Example #14
Source File: CriteriaSubqueryImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public <X, Y> ListJoin<X, Y> correlate(ListJoin<X, Y> source) {
	final ListJoinImplementor<X,Y> correlation = ( (ListJoinImplementor<X,Y>) source ).correlateTo( this );
	queryStructure.addCorrelationRoot( correlation );
	return correlation;
}
 
Example #15
Source File: CriteriaBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <X, T, E extends T> ListJoin<X, E> treat(ListJoin<X, T> join, Class<E> type) {
	return treat( join, type, (j, t) -> ((ListJoinImplementor) join).treatAs( type ) );
}
 
Example #16
Source File: JpaSoftwareModuleManagement.java    From hawkbit with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public Slice<AssignedSoftwareModule> findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc(
        final Pageable pageable, final long orderByDistributionId, final String searchText, final Long typeId) {

    final List<AssignedSoftwareModule> resultList = new ArrayList<>();
    final int pageSize = pageable.getPageSize();
    final CriteriaBuilder cb = entityManager.getCriteriaBuilder();

    // get the assigned software modules
    final CriteriaQuery<JpaSoftwareModule> assignedQuery = cb.createQuery(JpaSoftwareModule.class);
    final Root<JpaSoftwareModule> assignedRoot = assignedQuery.from(JpaSoftwareModule.class);
    assignedQuery.distinct(true);
    final ListJoin<JpaSoftwareModule, JpaDistributionSet> assignedDsJoin = assignedRoot
            .join(JpaSoftwareModule_.assignedTo);
    // build the specifications and then to predicates necessary by the
    // given filters
    final Predicate[] specPredicate = specificationsToPredicate(buildSpecificationList(searchText, typeId),
            assignedRoot, assignedQuery, cb,
            cb.equal(assignedDsJoin.get(JpaDistributionSet_.id), orderByDistributionId));
    // if we have some predicates then add it to the where clause of the
    // multi select
    assignedQuery.where(specPredicate);
    assignedQuery.orderBy(cb.asc(assignedRoot.get(JpaSoftwareModule_.name)),
            cb.asc(assignedRoot.get(JpaSoftwareModule_.version)));
    // don't page the assigned query on database, we need all assigned
    // software modules to filter
    // them out in the unassigned query
    final List<JpaSoftwareModule> assignedSoftwareModules = entityManager.createQuery(assignedQuery)
            .getResultList();
    // map result
    if (pageable.getOffset() < assignedSoftwareModules.size()) {
        assignedSoftwareModules
                .subList((int) pageable.getOffset(),
                        Math.min(assignedSoftwareModules.size(), pageable.getPageSize()))
                .forEach(sw -> resultList.add(new AssignedSoftwareModule(sw, true)));
    }

    if (assignedSoftwareModules.size() >= pageSize) {
        return new SliceImpl<>(resultList);
    }

    // get the unassigned software modules
    final CriteriaQuery<JpaSoftwareModule> unassignedQuery = cb.createQuery(JpaSoftwareModule.class);
    unassignedQuery.distinct(true);
    final Root<JpaSoftwareModule> unassignedRoot = unassignedQuery.from(JpaSoftwareModule.class);

    Predicate[] unassignedSpec;
    if (!assignedSoftwareModules.isEmpty()) {
        unassignedSpec = specificationsToPredicate(buildSpecificationList(searchText, typeId), unassignedRoot,
                unassignedQuery, cb, cb.not(unassignedRoot.get(JpaSoftwareModule_.id).in(
                        assignedSoftwareModules.stream().map(SoftwareModule::getId).collect(Collectors.toList()))));
    } else {
        unassignedSpec = specificationsToPredicate(buildSpecificationList(searchText, typeId), unassignedRoot,
                unassignedQuery, cb);
    }

    unassignedQuery.where(unassignedSpec);
    unassignedQuery.orderBy(cb.asc(unassignedRoot.get(JpaSoftwareModule_.name)),
            cb.asc(unassignedRoot.get(JpaSoftwareModule_.version)));
    final List<JpaSoftwareModule> unassignedSoftwareModules = entityManager.createQuery(unassignedQuery)
            .setFirstResult((int) Math.max(0, pageable.getOffset() - assignedSoftwareModules.size()))
            .setMaxResults(pageSize).getResultList();
    // map result
    unassignedSoftwareModules.forEach(sw -> resultList.add(new AssignedSoftwareModule(sw, false)));

    return new SliceImpl<>(resultList);
}
 
Example #17
Source File: ActionSpecifications.java    From hawkbit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Specification which joins all necessary tables to retrieve the dependency
 * between a target and a local file assignment through the assigned action
 * of the target. All actions are included, not only active actions.
 * 
 * @param controllerId
 *            the target to verify if the given artifact is currently
 *            assigned or had been assigned
 * @param sha1Hash
 *            of the local artifact to check wherever the target had ever
 *            been assigned
 * @return a specification to use with spring JPA
 */
public static Specification<JpaAction> hasTargetAssignedArtifact(final String controllerId, final String sha1Hash) {
    return (actionRoot, query, criteriaBuilder) -> {
        final Join<JpaAction, JpaDistributionSet> dsJoin = actionRoot.join(JpaAction_.distributionSet);
        final SetJoin<JpaDistributionSet, JpaSoftwareModule> modulesJoin = dsJoin.join(JpaDistributionSet_.modules);
        final ListJoin<JpaSoftwareModule, JpaArtifact> artifactsJoin = modulesJoin
                .join(JpaSoftwareModule_.artifacts);
        return criteriaBuilder.and(criteriaBuilder.equal(artifactsJoin.get(JpaArtifact_.sha1Hash), sha1Hash),
                criteriaBuilder.equal(actionRoot.get(JpaAction_.target).get(JpaTarget_.controllerId),
                        controllerId));
    };
}
 
Example #18
Source File: ActionSpecifications.java    From hawkbit with Eclipse Public License 1.0 3 votes vote down vote up
/**
 * Specification which joins all necessary tables to retrieve the dependency
 * between a target and a local file assignment through the assigned action
 * of the target. All actions are included, not only active actions.
 * 
 * @param targetId
 *            the target to verify if the given artifact is currently
 *            assigned or had been assigned
 * @param sha1Hash
 *            of the local artifact to check wherever the target had ever
 *            been assigned
 * @return a specification to use with spring JPA
 */
public static Specification<JpaAction> hasTargetAssignedArtifact(final Long targetId, final String sha1Hash) {
    return (actionRoot, query, criteriaBuilder) -> {
        final Join<JpaAction, JpaDistributionSet> dsJoin = actionRoot.join(JpaAction_.distributionSet);
        final SetJoin<JpaDistributionSet, JpaSoftwareModule> modulesJoin = dsJoin.join(JpaDistributionSet_.modules);
        final ListJoin<JpaSoftwareModule, JpaArtifact> artifactsJoin = modulesJoin
                .join(JpaSoftwareModule_.artifacts);
        return criteriaBuilder.and(criteriaBuilder.equal(artifactsJoin.get(JpaArtifact_.sha1Hash), sha1Hash),
                criteriaBuilder.equal(actionRoot.get(JpaAction_.target).get(JpaTarget_.id), targetId));
    };
}