What is the best query strategy and the best configuration for my entities when the second level cache is activated.
For example I have two entities User and Group with these relations :
One User to Many Group : the groups owned by the user. Here a collection of Group in the User class and a User attribute (the owner) in the Group class.
Many User to Many Group with attributes (status, date etc.) : the members of the groups. Because of the additionnals attibutes there is a specific class to map the intermediate table called UserGroup. There is a oneToMany collection of UserGroup in the User class and another one in the Group class.
The related attributes or collections are configured to be retrieved in Lazy mode. All the entities class and collections are cacheable.
So far if I wanted the user’s groups to be retrieved with their member I was making a request of this type :
select g from Group g left join fetch UserGroup ug left join fetch ug.user u where g.group.owner = :idOwner
Now with the second level cache that works as a key-value store, isn’t it easier and effective to get the user from his id and then let hibernate recover the groups and the members in Lazy mode to finally save the data in the second level cache ? The next query will use the cache, so what is the benefit of using a join ? Also I wouldn’t have to configure any optimized queries to be cached.