I created an util method:
public <T> Optional<T> fetch(Class<T> clazz, Object id, String... relations) {
EntityGraph<T> graph = entityManager.createEntityGraph(clazz);
Stream.of(relations).forEach(graph::addSubgraph);
return Optional.ofNullable(entityManager.find(clazz, id, Collections.singletonMap("javax.persistence.loadgraph", graph)));
}
So if for example if User has lazy orders and wallets, I can do this:
Optional<User> user = fetch(User.class, 1, "orders", "wallets");
But I don't know how to take orders's or wallets lazy collections. It would be greate if I would call method like this:
Optional<User> user = fetch(User.class, 1, "orders", "orders.products", "wallet");
How can I extend the method to achieve this?