title: Why does a JPA query return different objects
body: <p>Im using JPA in my Java ee application. I have a table A mapped to a class A. Now when I query the entity (e.g. by its id) it always returns a different object. Example:</p>
  
  <pre><code>public A getAById(int id) {
      A obj = (A) em.createNamedQuery("getAById")
              .setParameter("id", id).getSingleResult();
      return obj;
  }
  
  public void test(){
      getAById(1)==getAById(1) //is false
  }
  </code></pre>
  
  <p>Is there a way to tell JPA not to always create new instances when querying from a database but to return an existing object if it hast alreay been queried?</p>
  
  <p>//EDIT
  This article helped me a lot: 
  <a href="http://en.wikibooks.org/wiki/Java_Persistence/Caching#Example_JPA_2.0_Cacheable_annotation" rel="nofollow">http://en.wikibooks.org/wiki/Java_Persistence/Caching#Example_JPA_2.0_Cacheable_annotation</a></p>