I’m working on a query together that needs to load a list of entities that have a many-to-one relationship with another entity, which itself has a many-to-one relationship with another entity. I don’t need any of the data loaded by the association, but when reviewing the query log I’m finding that JPA is automatically loading it anyways. My understanding is that this happens because my JPA implementation can’t create a proxy to represent the associated entity like it can do with collections.
Let’s say my entity is Ham, which has a many-to-one association with a Sandwich. My JPQL to get a list of ham might look like so:
Select H from Ham H
Is there a way to explicitly ignore Ham’s association with sandwich in JPQL so my database doesn’t get pelted with a superfluous series of select sandwich queries without resorting to a join fetch?
When defining the ManyToOne relationship, mark it as “lazy loading” (by using anotations, it is something like):
That way the PQL above will work as expected. But, when you require Sandwich to be loaded, you will have to add a FETCH JOIN