Skip to content

Commit

Permalink
eagerly load the owner entity
Browse files Browse the repository at this point in the history
  • Loading branch information
barneyb committed May 19, 2024
1 parent 8ae75fd commit 6613dbd
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.brennaswitzer.cookbook.util.UserPrincipalAccess;
import com.google.common.annotations.VisibleForTesting;
import org.dataloader.BatchLoader;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -29,8 +30,10 @@ public class RecipeIsFavoriteBatchLoader implements BatchLoader<Recipe, Boolean>
@Override
public CompletionStage<List<Boolean>> load(List<Recipe> recipes) {
// Graphql will complete the future on a thread from its own pool,
// outside Spring's context, so retrieve the owner synchronously.
User owner = principalAccess.getUser();
// outside Spring's context, so retrieve the owner synchronously. It'll
// also be outside the Hibernate session, so eagerly load it. The owners
// of the Recipes should already have been loaded.
User owner = (User) Hibernate.unproxy(principalAccess.getUser());
return CompletableFuture.supplyAsync(() -> loadInternal(owner, recipes));
}

Expand Down

0 comments on commit 6613dbd

Please sign in to comment.