List of usage examples for javax.persistence.criteria Root join
<Y> Join<X, Y> join(SingularAttribute<? super X, Y> attribute, JoinType jt);
From source file:org.seedstack.i18n.rest.internal.KeyJpaFinder.java
/** * Extracts predicates from criteria./*from w ww . j a va 2 s . com*/ * * @param criteria criteria * @param cb criteria builder * @param k root key * @return list of predicate */ private Predicate[] getPredicates(Map<String, Object> criteria, CriteriaQuery q, CriteriaBuilder cb, Root<Key> k) { List<Predicate> predicates = new ArrayList<Predicate>(); if (criteria != null) { // extract criteria from the map Boolean isApprox = (Boolean) criteria.get(IS_APPROX); Boolean isMissing = (Boolean) criteria.get(IS_MISSING); Boolean isOutdated = (Boolean) criteria.get(IS_OUTDATED); String searchName = (String) criteria.get(SEARCH_NAME); // is the key LIKE searchName if (StringUtils.isNotBlank(searchName)) { predicates.add(cb.like(k.<String>get(ENTITY_ID), "%" + searchName + "%")); } // is the key outdated if (isOutdated != null) { predicates.add(cb.equal(k.<Boolean>get(OUTDATED), isOutdated)); } // if a default translation is available String defaultLocale = localeService.getDefaultLocale(); if (defaultLocale != null && isApprox != null) { // join translation table Join<Key, Translation> tln = k.join(TRANSLATIONS, JoinType.LEFT); // WHERE locale = default locale predicates.add(cb.equal(tln.get(ENTITY_ID).get(LOCALE), defaultLocale)); // is the translation approximate predicates.add(cb.equal(tln.get(APPROXIMATE), isApprox)); } // is the translation missing if (isMissing != null) { // SubQuery to find all the key which get a translation for the default locale //noinspection unchecked Subquery<String> subquery = q.subquery(String.class); Root<Key> fromKey = subquery.from(Key.class); subquery.select(fromKey.<String>get(ENTITY_ID)); Join join = fromKey.join(TRANSLATIONS, JoinType.LEFT); subquery.where(cb.and(cb.equal(join.get(ENTITY_ID).get(LOCALE), defaultLocale), cb.notEqual(join.get(VALUE), ""))); // Find all keys not in the above subquery, ie. all the keys missing predicates.add(cb.not(cb.in(k.get(ENTITY_ID)).value(subquery))); } } return predicates.toArray(new Predicate[predicates.size()]); }
From source file:org.seedstack.i18n.rest.internal.TranslationJpaFinder.java
/** * Extracts predicates from criteria./*from ww w .j a va 2 s . c o m*/ * * @param criteria criteria * @param cb criteria builder * @param k root key * @return list of predicate */ private Predicate[] getPredicates(Map<String, Object> criteria, CriteriaQuery q, CriteriaBuilder cb, Root<Key> k) { List<Predicate> predicates = new ArrayList<Predicate>(); if (criteria != null) { // extract criteria from the map Boolean isApprox = (Boolean) criteria.get(IS_APPROX); Boolean isMissing = (Boolean) criteria.get(IS_MISSING); Boolean isOutdated = (Boolean) criteria.get(IS_OUTDATED); String searchName = (String) criteria.get(SEARCH_NAME); String locale = (String) criteria.get(LOCALE); // is the key LIKE searchName if (StringUtils.isNotBlank(searchName)) { predicates.add(cb.like(k.<String>get(ENTITY_ID), "%" + searchName + "%")); } // if a default translation is available if (isApprox != null || isOutdated != null) { // join translation table Join<Key, Translation> tln = k.join(TRANSLATIONS, JoinType.LEFT); // WHERE locale = default locale predicates.add(cb.equal(tln.get(ENTITY_ID).get(LOCALE), locale)); // is the key outdated if (isOutdated != null) { predicates.add(cb.equal(tln.<Boolean>get(OUTDATED), isOutdated)); } // is the translation approximate if (isApprox != null) { predicates.add(cb.equal(tln.<Boolean>get(APPROXIMATE), isApprox)); } } // is the translation missing if (isMissing != null) { // SubQuery to find all the key which get a translation for the default locale //noinspection unchecked Subquery<String> subquery = q.subquery(String.class); Root<Key> fromKey = subquery.from(Key.class); subquery.select(fromKey.<String>get(ENTITY_ID)); Join join = fromKey.join(TRANSLATIONS, JoinType.LEFT); subquery.where(cb.and(cb.equal(join.get(ENTITY_ID).get(LOCALE), locale), cb.notEqual(join.get(VALUE), ""))); // Find all keys not in the above subquery, ie. all the keys missing predicates.add(cb.not(cb.in(k.get(ENTITY_ID)).value(subquery))); } } return predicates.toArray(new Predicate[predicates.size()]); }