Detecting N+1 problem using Statistics

I was planning on using Statistics methods getEntityLoadCount, getEntityFetchCount and to detect getQueryExecutionCount N+1 problem when there is a lazily fetched list for some entity.

Specifically for Spring Data using Repository interface with something like

@EntityGraph(attributePaths = {children})
Parent findById(Long id);

I was expecting to see a difference in query count or loads or selects, but no matter if EntityGraph annotation is used here the statistics are the same: the parent and children are included in loads, nothing is in fetches and there is one query only counted even if there would be a separate SQL select for the children table. So it does not count individual selects only what it seems to consider “queries”.

What would be the intended way with Statistics to check the select count like this, or is it even possible? Or is How to detect the Hibernate N+1 query problem during testing - Vlad Mihalcea the only way to do this?

You can use a org.hibernate.SessionEventListener or listen for the JFR events that Hibernate ORM can emit when using the hibernate-jfr module.