Search code examples
javaspring-data-jpaspring-dataquery-derivation

Spring data jpa method query findWith


Lately, I have come across this Spring Data JPA repository method findWithBooksById.

The two classes involved are very basic: Library one-to-many Books, and the method is querying for a library and its books.

I looked at https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.details , but there is no reference to this method pattern (findWith...).

Looking at the query generated, it queries the library table and queries books immediately after. So two queries are called consecutively like if I have called getBooks right after a findById query (lazily initialized books in this case).

Does anyone know how findWith... works in Spring Data JPA?


Solution

  • Does anyone know how "findWith..." works in Spring Data JPA?

    It doesn't. The pattern used is that of find...By....

    The second select is probably standard behavior of the JPA implementation used. It might be that Books get eagerly loaded but can't get loaded in the initial query or that something accesses them and thereby triggers lazy loading. It's impossible to tell without knowing the JPA implementation and the model classes involved.