title: Problem with Hibernate find method
body: <p><strong>UPDATE: EXAMPLE TO CLARIFY</strong> 
  I'm going to put an example of what's happening just to clarify the situation in my Spring + Hibernate application.
  Imagine I have this two entities (suppose getters and setters exists too)</p>
  
  <pre><code>@Entity
  public class Class1(){
    private Integer id;
  
    @OneToOne
    private Class2 object2;
  }
  
  @Entity
  public class Class2(){
    private Integer id;
  
    @OneToOne
    private Class1 object2;
  }
  </code></pre>
  
  <p>So in my database I have two tables to map every instance of each object. Suppose I have stored  in the database these objects:</p>
  
  <ul>
  <li>objectA (class Class1) : id = 1 , object2 = objectB</li>
  <li>objectB (class Class2) : id = 2 , object2 = objectA</li>
  <li>objectC (class Class2) : id = 3 , object2 = null
  which means A anb B are linked. Suppose now I want to link objectA to objectC. It means I have to delete the link in the objectB too.</li>
  </ul>
  
  <p>For doing this I do the folowing:</p>
  
  <pre><code>//I get the objectA from database
  Class1 storedObjectA = myservice.find(objectAId);
  //Now in storedObject1 I have the same as what it is stored as objectA
  
  //I change only the object2 attribute of it
  storedObject1.setObject2(objectC);
  
  //I search again in the database to see objectA
  Class1 reStoredObjectA = myservice.find(objectAId);
  </code></pre>
  
  <p>restoredObjectA now is: </p>
  
  <ul>
  <li>restoredObjectA (class = Class1) id = 1, object2 = objectC </li>
  </ul>
  
  <p>though in the database there is:</p>
  
  <ul>
  <li>restoredObjectA (class = Class1) id = 1, object2 = objectB </li>
  </ul>
  
  <p>The method find has not searched in the database and I have gotten the previous search though I have my query and second level caches disabled. The method find only goes to the DAO where it executes on the entityManager.find(Class1.class, id)
  How can I make my second find searchs in the database?</p>
  
  <p>Thanks</p>
  
  <hr>
  
  <p>(Full explanation)</p>
  
  <p>Hello,</p>
  
  <p>I have an application which uses Spring 3 and Hibernate. I have an entity named Class1 which is stored in a database. This entity has a OneToOne link to another entity called Class2. Class2 has another OneToOne Link to Class1, and both links have unique property set to true.  </p>
  
  <p>I have the following code in my controller:</p>
  
  <pre><code>//I have an object called dataFromUser which is instance of Class1 and
  // it is obtained via @ModelAttribute annotation from a form
  
  Integer id = dataFromUser.getId();
  
  //I get correctly the stored object from the database
  Class1 storedData = myService.find(id); //this will call em.find of hibernate
  
  //I set to the stored data the attribute I want to change
  storedData.setObject2();
  
  //I save again the object
  myService.save(storedData);
  </code></pre>
  
  <p>The method save in myService will use the entity manager to persist the data, but before that it will check the previous association between Class1 object and Class2 object to delete it in case a new Class2 Object has set to the instance of Class1. For that reason I have to call find method again to see what's in my database</p>
  
  <p>In the Service:</p>
  
  <pre><code>@Transactional
  public void save(Class1 object1){
     Class1 objectFromDB =  myDAO.find(object1.getId());
     // ....  update the old object and save
  }
  </code></pre>
  
  <p>The problem is that objectFromDB in the Service is not the same as when I did myService.find(id) in the controller. The problem is that it is not looking for it in the database but it is searching in memory. I know it because there is no SQL statement logged when the second find() method is called. As I had done setObject2() on the storedData object I get exactly this object when I call find method for the second time, and the database doesn't contain this because it hasn't been persisted yet.</p>
  
  <p>I have tried to disable Cache in my configuration just to try, but it is still happening. I have a ehCache with this configuration:</p>
  
  <p>In persistence.xml I have this properties (I have set to false both caches):</p>
  
  <pre><code>    &lt;properties&gt;
       &lt;!-- Hibernate 3.5.0 - try org.hibernate.cache.* package --&gt;
    &lt;property name="hibernate.cache.provider_class"
                 value="net.sf.ehcache.hibernate.SingletonEhCacheProvider" /&gt;
     &lt;property name="hibernate.cache.use_query_cache" value="false" /&gt;
    &lt;property name="hibernate.cache.use_second_level_cache" value="false" /&gt;
    &lt;property name="hibernate.generate_statistics" value="true" /&gt;
    &lt;property name="hibernate.cache.use_structured_entries" value="false" /&gt;
    &lt;property name="hibernate.format_sql" value="true" /&gt;
    &lt;!-- http://www.jroller.com/eyallupu/entry/hibernate_s_hbm2ddl_tool --&gt;
    &lt;!-- create, create-drop, update, validate --&gt;
    &lt;property name="hibernate.hbm2ddl.auto" value="update" /&gt;
      &lt;/properties&gt;  
  </code></pre>
  
  <p>In my ehcache.xml I have set to 0 maxElementsInMemory and overflowToDisk to false to disable it too.</p>
  
  <p>
  
   </p>
  
  <p>
  </p>
  
  <p>Is there any way to avoid this situation?</p>
  
  <p>Thanks.</p>