/** * @see DATAJPA-403 */ @Test public void reusesExistingJoinForExpression() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> from = query.from(User.class); PropertyPath managerFirstname = PropertyPath.from("manager.firstname", User.class); PropertyPath managerLastname = PropertyPath.from("manager.lastname", User.class); QueryUtils.toExpressionRecursively(from, managerLastname); QueryUtils.toExpressionRecursively(from, managerFirstname); assertThat(from.getJoins(), hasSize(1)); }
/** * @see DATAJPA-401 */ @Test public void doesNotCreateAJoinForNonOptionalAssociation() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Order> query = builder.createQuery(Order.class); Root<Order> root = query.from(Order.class); QueryUtils.toExpressionRecursively(root, PropertyPath.from("customer", Order.class)); }
/** * @see DATAJPA-454 */ @Test public void createsJoingToTraverseCollectionPath() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); QueryUtils.toExpressionRecursively(root, PropertyPath.from("colleaguesLastname", User.class)); assertThat(root.getJoins(), hasSize(1)); }
/** * @see DATAJPA-401 */ @Test public void createsJoinForOptionalAssociation() { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); QueryUtils.toExpressionRecursively(root, PropertyPath.from("manager", User.class)); assertThat(root.getJoins(), hasSize(1)); }
/** * Creates a criteria API {@link javax.persistence.criteria.Order} from the given {@link Order}. * * @param order the order to transform into a JPA {@link javax.persistence.criteria.Order} * @param root the {@link Root} the {@link Order} expression is based on * @param cb the {@link CriteriaBuilder} to build the {@link javax.persistence.criteria.Order} * with * @return */ @SuppressWarnings("unchecked") private static javax.persistence.criteria.Order toJpaOrder( Order order, Root<?> root, CriteriaBuilder cb) { PropertyPath property = PropertyPath.from(order.getProperty(), root.getJavaType()); Expression<?> expression = toExpressionRecursively(root, property); if (order.isIgnoreCase() && String.class.equals(expression.getJavaType())) { Expression<String> lower = cb.lower((Expression<String>) expression); return order.isAscending() ? cb.asc(lower) : cb.desc(lower); } else { return order.isAscending() ? cb.asc(expression) : cb.desc(expression); } }