While exploring my new project I have encountered some legacy code, which has the meaning of explicit prefetching some query results into Hibernate cache.
The task solved by that code is to select entity basing on some attribute value.
For example:
SELECT c
FROM MyEntity c
LEFT OUTER JOIN c.relatedEntity relatedEntity
WHERE c.field1 = :param1 AND relatedEntity.field1 = :param2 AND relatedEntity.field2 = :param3
The suggested improvement is to prefetch all the entities before running this query, i.e. run one of the following queries before the main one:
SELECT c, relatedEntity FROM MyEntity c LEFT OUTER JOIN c.relatedEntity relatedEntity
or
SELECT FROM MyEntity;
SELECT FROM RelatedEntity;
What do you think about the optimizations of this kind? Can it be useful? If "yes", in which situations?