@BatchMapping's api with JPA forces you to use spring.jpa.open-in-view=true
#579
Labels
status: waiting-for-triage
An issue we've not yet triaged
When you have a spring-data-jpa project with
and an 1:n relationship that you want to traverse via
@BatchMapping
.Lets assume the following data model
where 1 club has
n
athletesand the following mapping
Then trying to allow quering the
club.athletes
via Graphql@BatchMapping
causes the list of clubs passed in as a parameter to be from a different hibernate session than the athletes loaded in the current session.Therefore, the
athlete.club
will return a hibernate proxy which is not initialized at this point.Either you make the
@BatchMapping
method@Transactional
which causes theathlete.club
instances to be hydrated again (therefore, defeating the purpose of the@BatchMapping
as this is effectively an n+1 on theclub
table) or you simply get an exception that the hashcode of theathlete.club
cannot be calculated.I feel that the API for
@BatchMapping
is a bit unfortunate, it would have been better to allowthat way, you could use "projection DTO" interfaces (for the Athlete) which would avoid the hibernate lazy loading issue.
A sample repo showing this problem can be found at https://github.com/rburgst/spring-boot3-graphql-kotlin
See also spring-io/initializr#1359
The text was updated successfully, but these errors were encountered: