@Override public List<Product> readProductsByIds(List<Long> productIds) { if (productIds == null || productIds.size() == 0) { return null; } if (productIds.size() > 100) { logger.warn("Not recommended to use the readProductsByIds method for long lists of productIds, since " + "Hibernate is required to transform the distinct results. The list of requested" + "product ids was (" + productIds.size() +") in length."); } // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); Root<ProductImpl> product = criteria.from(ProductImpl.class); product.fetch("defaultSku", JoinType.LEFT); criteria.select(product); // We only want results that match the product IDs criteria.where(product.get("id").as(Long.class).in( sandBoxHelper.mergeCloneIds(ProductImpl.class, productIds.toArray(new Long[productIds.size()])))); TypedQuery<Product> query = em.createQuery(criteria); query.setHint(QueryHints.HINT_CACHEABLE, true); query.setHint(QueryHints.HINT_CACHE_REGION, "query.Catalog"); return query.getResultList(); }
protected CriteriaQuery<Product> getCriteriaForActiveProducts(Date currentDate, Long lastId) { // Set up the criteria query that specifies we want to return Products CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Product> criteria = builder.createQuery(Product.class); // The root of our search is Product Root<ProductImpl> product = criteria.from(ProductImpl.class); // We need to filter on active date on the sku Join<Product, Sku> sku = product.join("defaultSku"); product.fetch("defaultSku"); // Product objects are what we want back criteria.select(product); // Ensure the product is currently active List<Predicate> restrictions = new ArrayList<Predicate>(); attachActiveRestriction(currentDate, product, sku, restrictions); if (lastId != null) { restrictions.add(builder.gt(product.get("id").as(Long.class), lastId)); } // Add the restrictions to the criteria query criteria.where(restrictions.toArray(new Predicate[restrictions.size()])); //Add ordering so that paginated queries are consistent criteria.orderBy(builder.asc(product.get("id"))); return criteria; }
@Test public void test_criteria_from_fetch_example() { doInJPA( this::entityManagerFactory, entityManager -> { //tag::criteria-from-fetch-example[] CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Phone> criteria = builder.createQuery( Phone.class ); Root<Phone> root = criteria.from( Phone.class ); // Phone.person is a @ManyToOne Fetch<Phone, Person> personFetch = root.fetch( Phone_.person ); // Person.addresses is an @ElementCollection Fetch<Person, String> addressesJoin = personFetch.fetch( Person_.addresses ); criteria.where( builder.isNotEmpty( root.get( Phone_.calls ) ) ); List<Phone> phones = entityManager.createQuery( criteria ).getResultList(); //end::criteria-from-fetch-example[] assertEquals(2, phones.size()); }); }
@Test public void hhh13151Test() throws Exception { Session s = openSession(); // Prepare Query CriteriaBuilder cb = s.getCriteriaBuilder(); CriteriaQuery<SuperEntity> criteria = cb.createQuery( SuperEntity.class ); Root<SuperEntity> root = criteria.from( SuperEntity.class ); cb.treat( root, SubEntity.class ).fetch( "subField" ); // Execute Transaction tx = s.beginTransaction(); List<SuperEntity> result = s.createQuery( criteria ).getResultList(); tx.commit(); s.close(); // Check results SideEntity subField = ( (SubEntity) result.get( 0 ) ).getSubField(); String name = subField.getName(); assertTrue( name != null ); } }
CriteriaQuery<Employee> query = builder.createQuery( Employee.class ); Root<Employee> root = query.from( Employee.class ); root.fetch( "projects", JoinType.LEFT); query.select(root).where( builder.and(
/** Add the needed fetches to a critera query */ protected void addFetches(final Root<StylesheetUserPreferencesImpl> descriptorRoot) { descriptorRoot.fetch(StylesheetUserPreferencesImpl_.layoutAttributes, JoinType.LEFT); descriptorRoot.fetch(StylesheetUserPreferencesImpl_.outputProperties, JoinType.LEFT); descriptorRoot.fetch(StylesheetUserPreferencesImpl_.parameters, JoinType.LEFT); }
@Override protected void prepareFetch(Root<T> root, QueryConfig config) { for (String path : fetchAssociations) { root.fetch(path); } } }
@Override protected void addFetches(Root<PortletExecutionAggregationImpl> root) { root.fetch(PortletExecutionAggregationImpl_.statisticalSummary, JoinType.LEFT); }
@Override protected void addFetches(Root<LoginAggregationImpl> root) { root.fetch(LoginAggregationImpl_.uniqueStrings, JoinType.LEFT); }
@Override protected void addFetches(Root<TabRenderAggregationImpl> root) { root.fetch(TabRenderAggregationImpl_.statisticalSummary, JoinType.LEFT); }
@Override protected void addFetches(Root<ConcurrentUserAggregationImpl> root) { root.fetch(ConcurrentUserAggregationImpl_.uniqueStrings, JoinType.LEFT); }
@Override @SuppressWarnings("hiding") public <X, Y> Fetch<X, Y> fetch(String attributeName, JoinType jt) { return getWrapped().fetch(attributeName, jt); }
public void createJoinInRoot(final String joinName, final JoinType joinType, final boolean isFetch) { if (isFetch) { this.joinFetch = root.fetch(joinName, joinType); return; } this.join = root.join(joinName, joinType); }
@Override @SuppressWarnings("hiding") public <X, Y> Fetch<X, Y> fetch(String attributeName) { return getWrapped().fetch(attributeName); }
/** Add all the fetches needed for completely loading the object graph */ protected void addFetches(final Root<PortletEntityImpl> definitionRoot) { definitionRoot .fetch(PortletEntityImpl_.portletPreferences, JoinType.LEFT) .fetch(PortletPreferencesImpl_.portletPreferences, JoinType.LEFT) .fetch(PortletPreferenceImpl_.values, JoinType.LEFT); definitionRoot.fetch(PortletEntityImpl_.windowStates, JoinType.LEFT); }
@Override public <Y> Fetch<X, Y> fetch(SingularAttribute<? super X, Y> attribute, JoinType jt) { return getWrapped().fetch(attribute, jt); }
@Override public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) { if (!Number.class.isAssignableFrom(query.getResultType())) { // do not join in count queries for (String path : pathsToFetch){ root.fetch(path, joinType); } } return null; }
@Override public <Y> Fetch<X, Y> fetch(PluralAttribute<? super X, ?, Y> attribute) { return getWrapped().fetch(attribute); }
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); } } }
@Override protected void prepareFetch(Root<T> root, QueryConfig config) { for (ConditionalCriteria criteria : conditionalCriterias) { if (Operator.FetchEager.equals(criteria.getOperator())) { // TODO: this is not tested root.fetch(criteria.getPropertyFullName()); } else if (Operator.FetchLazy.equals(criteria.getOperator())) { // TODO: fetchLazy is not supported actually } } }