spring-projects / spring-data-jpa

Simplifies the development of creating a JPA-based data access layer.
https://spring.io/projects/spring-data-jpa/
Apache License 2.0
3.02k stars 1.42k forks source link

Implement class-based DTOs for Fluent API #2327

Open gregturn opened 3 years ago

gregturn commented 3 years ago

2294 introduced the Fluent API. However, scheduling issues prevented us from implementing class-based DTOs.

As part of that development effort, the following chunk of code from FetchableFluentQueryByExample was withdrawn since it isn't relevant to everything.

    /**
     * Draft version of a projection-based query using class-based DTOs.
     *
     * @param sort
     * @param queryType
     * @param example
     * @return
     */
    private TypedQuery<R> createProjectionQueryByExample(Sort sort, Class<R> queryType, Example<S> example) {

        CriteriaBuilder builder = this.entityManager.getCriteriaBuilder();
        CriteriaQuery<R> query = builder.createQuery(queryType);

        Root<R> root = query.from(queryType);
        query.select(root);

        Predicate predicate = QueryByExamplePredicateBuilder.getPredicate(
                builder.createQuery(example.getProbeType()).from(example.getProbeType()), builder, example, escapeCharacter);

        if (predicate != null) {
            query.where(predicate);
        }

        if (sort.isSorted()) {
            query.orderBy(toOrders(sort, root, builder));
        }

        return this.entityManager.createQuery(query);
    }
gregturn commented 2 years ago

Related: https://github.com/spring-projects/spring-data-jpa/commit/8cd1388318032ddcdeeeadf347f691ed3a03e583 Related: https://github.com/spring-projects/spring-data-jpa/issues/487