title: Hibernate - query caching/second level cache does not work by value object containing subitems
body: <p>I have been struggling with the following problem:<br>
  I have a value object containing different panels. Each panel has a list of fields.</p>
  
  <p><strong>Mapping:</strong></p>
  
  <pre><code>&lt;class name="com.aviseurope.core.application.RACountryPanels" table="CTRY" schema="DBDEV1A" where="PEARL_CTRY='Y'" lazy="join"&gt;
  &lt;cache usage="read-only"/&gt;
  &lt;id name="ctryCode"&gt;
  &lt;column name="CTRY_CD_ID" sql-type="VARCHAR2(2)" not-null="true"/&gt;
  &lt;/id&gt;
  &lt;bag name="panelPE" table="RA_COUNTRY_MAPPING" fetch="join" where="MANDATORY_FLAG!='N'"&gt;
  &lt;key column="COUNTRY_LOCATION_ID"/&gt;
  &lt;many-to-many class="com.aviseurope.core.application.RAFieldVO" column="RA_FIELD_MID" where="PANEL_ID='PE'"/&gt;
  &lt;/bag&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p><strong>I use the following criteria to get the value object:</strong></p>
  
  <pre><code>Session m_Session = HibernateUtil.currentSession();
              m_Criteria = m_Session.createCriteria(RACountryPanels.class);
              m_Criteria.add(Expression.eq("ctryCode", p_Country));
              m_Criteria.setCacheable(true);
  </code></pre>
  
  <p>As I see the query cache contains only the main select like  </p>
  
  <pre><code>select * from CTRY where ctry_cd_id=?
  </code></pre>
  
  <p>Both RACountryPanels and RAFieldVO are second level cached.
  If I check the 2nd level cache content I can see that it cointains the RAFields and the RACountryPanels as well and I can see the select .. from CTRY where ctry_cd_id=... in query cache region as well.</p>
  
  <p><strong>When I call the servlet it seems that it is using the cache, but second time not.</strong> 
  If I check the content of the cache using JMX, everything seems to be ok, but when I measure the object access time, it seems that it does not always use the cache. </p>
  
  <p>Cheers
  Zoltan</p>