title: Hibernate second-level cache composite-id
body: <p>I am trying to cache an object in Hibernate's second level cache that has a composite-id mapped in my persistence-mapping file.  The logs say that the first time I run the query, the class mapped as the composite-id is put in the cache.  However, when I run the query a second time, the object is not pulled from the cache.  It is instead running the query again.</p>
  
  <p>Does Hibernate have a problem with second-level caching composite ids?</p>
  
  <p>Relevant info:</p>
  
  <ol>
  <li>Using Hibernate 3.1, ehcache 2.4.2</li>
  <li>Composite ID class implements serializable</li>
  <li>I am using a new Hibernate session when running the query a second time </li>
  <li>I am using hibernateTemplate.load(Class, ID) to retrieve the object</li>
  </ol>
  
  <p>This is how I'm constructing my ID and executing my query:</p>
  
  <pre><code>CompositeId id = new CompositeId(date, sessionId);
  UserDetails user = (UserDetails) hibernateTemplate.load(UserDetails.class, id);
  </code></pre>
  
  <p>And this is how my persistence-mapping file defines the above:</p>
  
  <pre><code>&lt;class name="com.entities.UserDetails"
          table="USER_DETAILS" 
          lazy="false"&gt;
      &lt;cache usage="read-write"/&gt;
  
      &lt;composite-id name="userId" class="com.entities.CompositeId" unsaved-value="undefined"&gt;
          &lt;key-property name="userSessionId" column="SESSION_ID" /&gt;
          &lt;key-property name="dateCreated" column="DATE_CREATED" type="date" /&gt;
      &lt;/composite-id&gt;
  </code></pre>
  
  <p>EDIT:  The plot thickens.... </p>
  
  <p>When I changed this to read-only cache policy, it worked fine.  Transactional cache behavior seems to be extremely unpredictable.  Can anyone explain why the above happened with a read-write cache, yet worked fine with read-only?  This table is not being updated so not sure why transactional semantics would change things in that instance.</p>