Background :
My application is using JPA Criteria to fetch the data from DB and a permanence issue in identified related to too many SQL calls(for child entities) to DB.
Take an example here:
ENTITY_A (parent) with 500 item types record
ENTITY_B (child-1) with 1000 item details record
ENTITY_C (child-2) with 1000 item details record
ENTITY_D (child-3) with 1000 item details record
As per requirement, We need to fetch data for parent entity along with its child(all entities). Child Entities are already mapped with fetch=FetchType.LAZY
in parent Entity.
Problem :
When we try to fetch 500 item types(ENTITY_A) , then the number of SQL calls are invoked in following manner :
- 1 Call for Entity_A List(to fetch all 500 records)
- 500 SQL calls for Entity_B List (to fetch records for the linked parent ID)
- 500 SQL calls for Entity_C List (to fetch records for the linked parent ID)
- 500 SQL calls for Entity_D List (to fetch records for the linked parent ID)
I know one alternate is to switch from Criteria to SQL Procedure call (which will return all the data finally with a single SQL call in form of SQL Types )
Is there an alternate way using which we can limit the SQL calls to DB in this manner?