Unexpected query

Hi,

I am using QuickPerf to monitor the number of queries that are executed behind the scenes. I noticed that in one of my scenario’s a, for me, unexpected query is executed. I am trying to understand it but I don’t get it. Perhaps somebody here knows the answer.

So I have a root entity named Project with the following relations:

  1. One to one with Client (Eager)
  2. One to one with Building (Eager)
  3. One to many with Window (Lazy)
  4. Many to one with Workspace (Eager)

I try to select all projects with some specific conditions + the number of windows.

        CriteriaQuery<Tuple> criteriaQuery = criteriaBuilder.createTupleQuery();
        Root<Project> from = criteriaQuery.from(Project.class);
        from.fetch("client", JoinType.LEFT);
        from.fetch("building", JoinType.LEFT);
        from.fetch("workspace", JoinType.LEFT);
        criteriaQuery.select(criteriaBuilder.tuple(from, criteriaBuilder.size(from.get("windows"))));
        criteriaQuery.groupBy(from);

        applyConditions(projectFilter, criteriaBuilder, criteriaQuery, from);

        return query.getResultStream().map(this::combineProjectAndWindowTotal);

I have a unit test that creates 4 projects. The test should find 3 projects. For each project that was found I print the number of windows (so I iterate over the full result list).

I can see that 2 queries are executed:

  1. The select query to find the projects and the number of windows.
  2. A query to load a project - but only for the first project in the result list.

Why the second query? And why only for the first project in the result list?

I would have expected only the first query OR the second query for EVERY project in the result list.

Thanks