I have many to one relation, let's say User has a Company. Company entity does not have any other relations.
If I enable second level cache for findAll hibernate query of Company, the second time I reload page (which means that User is loaded and then also list of all Companies is loaded) I get select for each existing company (select ... from company where id=?) in hibernate output. This happens when calling findAll for Company, it looks like this (this is generic method in class which is extended with appropriate type):
return (List<T>) getHibernateTemplate().execute(new HibernateCallback() {
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria c = session.createCriteria(persistentClass);
c.setCacheable(cacheFindAll);
return c.list();
}
});
I am using Jboss TreeCacheProvider in read-only strategy. If I use setCacheable(false) there are no "unwanted" selects.
Why is this happening and how can I eliminate this behavior?