I have problems with full loading of very complex object from DB in a reasonable time and with reasonable count of queries.
My object has a lot of embedded entities, each entity has references to another entities, another entities references yet another and so on (So, the nesting level is 6)
So, I’ve created example to demonstrate what I want:
I have User.
@OneToMany collections of favorite Oranges,Apples,Grapevines and Peaches. Each Grapevine has
@OneToMany collection of Grapes. Each fruit is another entity with just one String field.
I’m creating user with 30 favorite fruits of each type and each grapevine has 10 grapes. So, totally I have 421 entity in DB – 30*4 fruits, 100*30 grapes and one user.
And what I want: I want to load them using no more than 6 SQL queries.
And each query shouldn’t produce big result set (big is a result set with more that 200 records for that example).
My ideal solution will be the following:
6 requests. First request returns information about user and size of result set is 1.
Second request return information about Apples for this user and size of result set is 30.
Third, Fourth and Fifth requests returns the same, as second (with result set size = 30) but for Grapevines, Oranges and Peaches.
Sixth request returns Grape for ALL grapevines
This is very simple in SQL world, but I can’t achieve such with JPA (Hibernate).
I tried following approaches:
Use fetch join, like
from User u join fetch u.oranges .... This is awful. The result set is 30*30*30*30 and execution time is 10 seconds. Number of requests = 3. I tried it without grapes, with grapes you will get x10 size of result set.
Just use lazy loading. This is the best result in this example (with @Fetch=
SUBSELECT for grapes). But in that case that I need to manually iterate over each collection of elements. Also, subselect fetch is too global setting, so I would like to have something which could work on query level. Result set and time near ideal. 6 queries and 43 ms.
Loading with entity graph. The same as fetch join but it also make request for every grape to get it grapevine. However, result time is better (6 seconds), but still awful. Number of requests > 30.
I tried to cheat JPA with “manual” loading of entities in separate query. Like:
SELECT u FROM User where id=1;
SELECT a FROM Apple where a.user_id=1;
This is a little bit worse that lazy loading, since it requires two queries for each collection: first query to manual loading of entities (I have full control over this query, including loading associated entities), second query to lazy-load the same entities by Hibernate itself (This is executed automatically by Hibernate)
Execution time is 52, number of queries = 10 (1 for user, 1 for grape, 4*2 for each fruit collection)
Actually, “manual” solution in combination with SUBSELECT fetch allows me to use “simple” fetch joins to load necessary entities in one query (like
@OneToOne entities) So I’m going to use it. But I don’t like that I have to perform two queries to load collection.
I’m going to suggest yet another option on how to lazily fetch collections of Grapes in Grapevine:
@BatchSize(size = 30)
private List<Grape> grapes = new ArrayList<>();
Instead of doing a sub-select this one would use
in (?, ?, etc) to fetch many collections of
Grapes at once. Instead
? Grapevine IDs will be passed. This is opposed to querying 1
List<Grape> collection at a time.
That’s just yet another technique to your arsenal.