Spring Data Relational: Introducing Single Query Loading
The article introduces Single Query Loading in Spring Data JDBC 3.2.0-M2. Single Query Loading allows developers to load arbitrary aggregates with a single select statement, addressing the N+1 problem. To enable Single Query Loading, developers need to call setSingleQueryLoadingEnabled(true) on their RelationalMappingContext. Currently, this feature only works for simple aggregates, consisting of an aggregate root and a single collection of other entities, and is limited to the findAll, findById, and findAllByIds methods in CrudRepository. However, future versions will improve on these limitations. It is important to note that the database used must support analytic functions. The article also discusses the problem of loading complete aggregates in one go and the inefficiency of multiple SQL statements. The author proposes the idea of representing aggregates as trees in SQL queries. Overall, Single Query Loading in Spring Data JDBC offers a more efficient way to load aggregates and addresses the N+1 problem.