title: dirty reads of associations in hibernate second level cache
body: <p>When enabling ehCache  (2.7.0) as Hibernate (4.3.7) Second level cache, Hibernate returns the old collection association.</p>
  
  <p><strong>Model:</strong> A Member has a Wallet with Wallet transactions.</p>
  
  <p><strong>Scenario:</strong> a Wallet transaction is added to a Member in a transaction (with ehcache enabled). However, after the scenario (after the commit), the Wallet transaction isn't present in the Member, whereas it's present in the db.</p>
  
  <p><strong>Scenarios testing Code:</strong></p>
  
  <pre><code>startTransaction(); // used to create a transaction through Spring.
  member = findMemberById(); // Hibernate "get()" to retrieve member from Db.
  final WalletTransaction walTx = member.getEnsureWallet().addWalletTransaction(10); // add wallet tx of 10 euro.
  member.saveOrUpdate(); // will update the member and the wallet transactions through cascading
  commitTransaction();
  
  // assert wallet transaction is present
  startTransaction();
  final Taxer mem = findMemberById(member.getId()); // refresh member in session through it's PK, logging indicates it comes from cache.
  // final Taxer mem = findMemberByLoginName(member.getLoginName()); // when retrieving the member through it's loginName, the test works.
  assertTrue(mem.containsWalletTransaction(walTx)); // FAILS
  commitTransaction();
  </code></pre>
  
  <p><strong>The hibernate model member snippet:</strong></p>
  
  <pre><code>&lt;class name="com.core.domain.MemberDefault" table="mem" discriminator-value="Mem" &gt;
     &lt;component name="wallet" class="com.core.domain.Wallet"&gt;
          &lt;set name="transactions" table="wallet_tx" cascade="save-update, delete" &gt;
              &lt;!--cache usage="read-write" /--&gt;
              &lt;key column="idMember" not-null="true" /&gt;
  
              &lt;composite-element class="com.core.domain.WalletTransactionDefault"&gt;
                &lt;property name="amount" type="big_decimal" column="amount" /&gt;
                 .... (more props)
              &lt;/composite-element&gt;
          &lt;/set&gt;
    &lt;/component&gt;
  &lt;/class
  </code></pre>
  
  <p><strong>The MemberDefault and WalletDefault class snippets:</strong></p>
  
  <pre><code>public class MemberDefault implements Member {
    private Wallet wallet;
  ....
  }
  
  public class WalletDefault implements Wallet {
   private Set&lt;WalletTransaction&gt; transactions;
  
  public void setTransactions(Set&lt;WalletTransaction&gt; transactions) {
    this.transactions = transaction;
   }
  
   public Set&lt;WalletTransaction&gt; getTransactions() {
    return this.transactions;
   }
  }
  </code></pre>
  
  <p>Notes:</p>
  
  <ol>
  <li>In case I not add the wallet transaction in a transaction (remove the first start/commit), the test is performed with success (The above is isolated testing code to reproduce bug). </li>
  <li>In case I turn off the second-level, the test works.</li>
  <li>In case I not retrieve the member from the Db through it's PK, but through it's loginName, such that a query is used by Hibernate, and as such the query cache, the test works.</li>
  </ol>
  
  <p>I debugged, enabled hibernate/ehcache debug logging, modified the cache settings in hibernate, tried older Hibernate 4 and ehCache version, but don't seem to solve it, a bit frustrating. </p>
  
  <p>Please some advice on how to solve this?</p>