/**
	 * @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));
	}
Example #3
0
  /**
   * Reads the given {@link TypedQuery} into a {@link Page} applying the given {@link Pageable} and
   * {@link Specification}.
   *
   * @param query must not be {@literal null}.
   * @param spec can be {@literal null}.
   * @param pageable can be {@literal null}.
   * @return
   */
  private Page<M> readPage(TypedQuery<M> query, Pageable pageable, Specification<M> spec) {

    query.setFirstResult(pageable.getOffset());
    query.setMaxResults(pageable.getPageSize());

    Long total = QueryUtils.executeCountQuery(getCountQuery(spec));
    List<M> content =
        total > pageable.getOffset() ? query.getResultList() : Collections.<M>emptyList();

    return new PageImpl<M>(content, pageable, total);
  }
	/**
	 * @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));
	}
  private TypedQuery<M> getQuery(Specification<M> spec, Sort sort) {
    CriteriaBuilder builder = this.em.getCriteriaBuilder();
    CriteriaQuery query = builder.createQuery(this.entityClass);

    Root root = applySpecificationToCriteria(spec, query);

    query.select(root);

    applyJoins(root);

    if (sort != null) {
      query.orderBy(QueryUtils.toOrders(sort, root, builder));
    }

    TypedQuery q = this.em.createQuery(query);

    this.repositoryHelper.applyEnableQueryCache(q);

    return applyLockMode(q);
  }
  // 動的条件に一致するEntityを全件検索 するサンプルを元に実装
  //
  // http://terasolunaorg.github.io/guideline/5.0.1.RELEASE/ja/ArchitectureInDetail/DataAccessJpa.html#id21
  @Override
  public Page<Result> findByCriteria(ResultCriteria criteria, Pageable pageable) {

    // collect dynamic conditions.
    final List<String> andConditions = new ArrayList<String>();
    final List<String> joinConditions = new ArrayList<String>();
    final Map<String, Object> bindParameters = new HashMap<String, Object>();

    if (criteria.getStaffId() != null) {
      joinConditions.add("r.resultDetails rd");
      andConditions.add("rd.staff.id = :staffId");
      bindParameters.put("staffId", criteria.getStaffId());
    }

    // TODO result の検索条件はここに書く
    //        if (criteria.getStaffId() != null) {
    //            andConditions.add("d.staffId = :staffId");
    //            bindParameters.put("staffId", criteria.getStaffId());
    //        }

    //        if (!CollectionUtils.isEmpty(criteria.getStatusCodes())) {
    //            andConditions.add("o.status.code IN :statusCodes");
    //            bindParameters.put("statusCodes", criteria.getStatusCodes());
    //        }
    //        if (andConditions.isEmpty()) {
    //            List<Result> Results = Collections.emptyList();
    //            return new PageImpl<Result>(Results, pageable, 0); // (3)
    //        }

    // create dynamic query.
    final StringBuilder queryString = new StringBuilder();
    final StringBuilder countQueryString = new StringBuilder(); // (4)
    final StringBuilder conditionsString = new StringBuilder(); // (4)

    queryString.append("select r FROM Result r ");
    countQueryString.append("SELECT COUNT(r) FROM Result r ");

    // (11)
    // add join conditions.
    for (String joinCondition : joinConditions) {
      conditionsString.append(" JOIN ").append(joinCondition);
    }
    // add conditions.
    Iterator<String> andConditionsIt = andConditions.iterator();
    if (andConditionsIt.hasNext()) {
      conditionsString.append(" WHERE ").append(andConditionsIt.next());
    }
    while (andConditionsIt.hasNext()) {
      conditionsString.append(" AND ").append(andConditionsIt.next());
    }
    queryString.append(conditionsString); // (6)
    countQueryString.append(conditionsString); // (6)

    // add Result by condition.
    // (7)
    String query = QueryUtils.applySorting(queryString.toString(), pageable.getSort(), "r");

    // create typed query.
    final TypedQuery<Long> countQuery =
        entityManager.createQuery(countQueryString.toString(), Long.class); // (8)

    final TypedQuery<Result> findQuery = entityManager.createQuery(query, Result.class);

    // bind parameters.
    for (Map.Entry<String, Object> bindParameter : bindParameters.entrySet()) {
      countQuery.setParameter(bindParameter.getKey(), bindParameter.getValue()); // (8)
      findQuery.setParameter(bindParameter.getKey(), bindParameter.getValue());
    }

    long total = countQuery.getSingleResult().longValue(); // (9)
    List<Result> Results = null;
    if (total != 0) { // (10)
      findQuery.setFirstResult(pageable.getOffset());
      findQuery.setMaxResults(pageable.getPageSize());
      // execute query.
      Results = findQuery.getResultList();
    } else { // (11)
      Results = Collections.emptyList();
    }

    return new PageImpl<Result>(Results, pageable, total); // (12)
  }