# Unexpected query

**URL:** <https://discourse.hibernate.org/t/unexpected-query/3993>\
**Category:** Hibernate ORM\
**Created:** [April 8, 2020, 2:42pm UTC](https://discourse.hibernate.org/t/unexpected-query/3993 "2020-04-08T14:42:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Philippe\_Schottey](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/philippe_schottey/32/1018_2.png) [@Philippe\_Schottey](https://discourse.hibernate.org/u/Philippe_Schottey)\
**Post date:** [April 8, 2020, 2:42pm UTC](https://discourse.hibernate.org/t/unexpected-query/3993/1 "2020-04-08T14:42:08Z")

</div>

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.

```auto
        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
