I see a lot of posts where Eager fetch performs left join of child table parent table in hibernate. But when I use springboot , hibernate fires seperate sql queries - means one
The findByCustomerId
will actually generate a query based on that method instead of using em.find
. It will create something along the lines of SELECT c FROM Customer c WHERE c.customerId=:customerId
. afterwards it will notice the fetch strategy and obtain the needed references. This is also explained here. The query will do exactlly what you instruct it to do.
If you want to eagerly load the reference you would need to write the query yourself along the lines of SELECT c FROM Customer c JOIN FETCH c.orders o WHERE c.customerId=:customerId
, this will automatically retrieve the orders.
However the customerId
is actually the primary key or identitifier for your entity and thus you should actually be using the findById
or findOne
method (depending on your Spring Data JPA version). This will use the EntityManager.find which should take the mapping information into account and create the appropriate query.