default HarvestQuota findByHarvestSeasonAndRhy(final long harvestSeasonId, final long rhyId) { return findOne((root, query, cb) -> { final Join<HarvestQuota, HarvestSeason> seasonToJoin = root.join(HarvestQuota_.harvestSeason, JoinType.LEFT); final Predicate harvestSeasonPredicate = cb.equal(seasonToJoin.get(HarvestSeason_.id), harvestSeasonId); final Join<HarvestQuota, HarvestArea> areaToJoin = root.join(HarvestQuota_.harvestArea, JoinType.LEFT); final SetJoin<HarvestArea, Riistanhoitoyhdistys> rhysToJoin = areaToJoin.join(HarvestArea_.rhys, JoinType.LEFT); final Predicate rhyPredicate = cb.equal(rhysToJoin.get(Organisation_.id), rhyId); return cb.and(harvestSeasonPredicate, rhyPredicate); }); }
@Test public void getBooks() { log.info("... getBooks ..."); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> cq = cb.createQuery(Book.class); Root<Book> root = cq.from(Book.class); SetJoin<Book, Author> authors = root.join(Book_.authors); ParameterExpression<String> paramFirstName = cb.parameter(String.class); ParameterExpression<String> paramLastName = cb.parameter(String.class); cq.where( cb.and( cb.equal(authors.get(Author_.firstName), paramFirstName), cb.equal(authors.get(Author_.lastName), paramLastName))); TypedQuery<Book> query = em.createQuery(cq); query.setParameter(paramFirstName, "Thorben"); query.setParameter(paramLastName, "Janssen"); List<Book> books = query.getResultList(); Assert.assertEquals(1, books.size()); for (Book b : books) { log.info(b); } em.getTransaction().commit(); em.close(); }
/** * {@link Specification} for retrieving {@link DistributionSet}s by "has at * least one of the given tag names". * * @param tagNames * to be filtered on * @param selectDSWithNoTag * flag to select distribution sets with no tag * @return the {@link DistributionSet} {@link Specification} */ public static Specification<JpaDistributionSet> hasTags(final Collection<String> tagNames, final Boolean selectDSWithNoTag) { return (targetRoot, query, cb) -> { final SetJoin<JpaDistributionSet, JpaDistributionSetTag> tags = targetRoot.join(JpaDistributionSet_.tags, JoinType.LEFT); final Predicate predicate = getPredicate(tags, tagNames, selectDSWithNoTag, cb); query.distinct(true); return predicate; }; }
private static Predicate getPredicate(final SetJoin<JpaDistributionSet, JpaDistributionSetTag> tags, final Collection<String> tagNames, final Boolean selectDSWithNoTag, final CriteriaBuilder cb) { tags.get(JpaDistributionSetTag_.name); final Path<String> exp = tags.get(JpaDistributionSetTag_.name); if (selectDSWithNoTag != null && selectDSWithNoTag) { if (!CollectionUtils.isEmpty(tagNames)) { return cb.or(exp.isNull(), exp.in(tagNames)); } else { return exp.isNull(); } } else { return exp.in(tagNames); } }
/** * {@link Specification} for retrieving {@link DistributionSet}s by tag. * * @param tagId * the ID of the distribution set which must be assigned * @return the {@link DistributionSet} {@link Specification} */ public static Specification<JpaDistributionSet> hasTag(final Long tagId) { return (targetRoot, query, cb) -> { final SetJoin<JpaDistributionSet, JpaDistributionSetTag> tags = targetRoot.join(JpaDistributionSet_.tags, JoinType.LEFT); return cb.equal(tags.get(JpaDistributionSetTag_.id), tagId); }; }
private static Predicate getPredicate(final Root<JpaTarget> targetRoot, final CriteriaBuilder cb, final Boolean selectTargetWithNoTag, final String[] tagNames) { final SetJoin<JpaTarget, JpaTargetTag> tags = targetRoot.join(JpaTarget_.tags, JoinType.LEFT); final Path<String> exp = tags.get(JpaTargetTag_.name); if (selectTargetWithNoTag) { if (tagNames != null) { return cb.or(exp.isNull(), exp.in(tagNames)); } else { return exp.isNull(); } } else { return exp.in(tagNames); } }
/** * {@link Specification} for retrieving {@link Target}s by tag. * * @param tagId * the ID of the distribution set which must be assigned * @return the {@link Target} {@link Specification} */ public static Specification<JpaTarget> hasTag(final Long tagId) { return (targetRoot, query, cb) -> { final SetJoin<JpaTarget, JpaTargetTag> tags = targetRoot.join(JpaTarget_.tags, JoinType.LEFT); return cb.equal(tags.get(JpaTargetTag_.id), tagId); }; }
@Override public <Y> SetJoin<X, Y> join(SetAttribute<? super X, Y> set) { // TODO Auto-generated method stub return null; }
@Override public <Y> SetJoin<X, Y> join(SetAttribute<? super X, Y> set, JoinType jt) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> SetJoin<X, Y> joinSet(String attributeName) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> SetJoin<X, Y> joinSet(String attributeName, JoinType jt) { // TODO Auto-generated method stub return null; }
@Override public <X, T, E extends T> SetJoin<X, E> treat(SetJoin<X, T> join, Class<E> type) { // TODO Auto-generated method stub return null; }
@Override @SuppressWarnings("unchecked") public <X, T, E extends T> SetJoin<X, E> treat(SetJoin<X, T> join, Class<E> type) { // return ( (SetJoinImplementor) join ).treatAs( type ); throw new NotYetImplementedException( ); }
@Override public <Y> SetJoin<T, Y> join( SetAttribute<? super T, Y> arg0 ) { // TODO Auto-generated method stub return null; }
@Override public <Y> SetJoin<T, Y> join( SetAttribute<? super T, Y> arg0, JoinType arg1 ) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> SetJoin<X, Y> joinSet( String arg0 ) { // TODO Auto-generated method stub return null; }
@Override public <X, Y> SetJoin<X, Y> joinSet( String arg0, JoinType arg1 ) { // TODO Auto-generated method stub return null; }
/** * 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)); }; }
/** * 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)); }; }