title: EntityManager.find behavior with an object containing 2 id as primary key
body: <p>I'm starting to step into the Java world and I'm having troubles to understand this.</p>
  
  <p>I'm using JPA to interact with my database with JPQL.</p>
  
  <p>I have a persistent class like this one :</p>
  
  <pre><code>@Entity
  @Table(name="C_A_T")
  @NamedQuery(name="CAT.findAll", query="SELECT c FROM CAT c")
  public class CAT implements Serializable {
      private static final long serialVersionUID = 1L;
      @EmbeddedId
      private CATPK id;
      private String offer;
      @Temporal(TemporalType.DATE)
      @Column(name="MODIF_DATE")
      private Date modifDate;
  
  
      public CAT() {}
      public CATPK getId() {return id;}
      public void setId(CATPK id) {this.id = id;}
      public String getOffer() {return offer;}
      public void setOffer(String offer) {this.offer = offer;} 
      public Date getModifDate() {return modifDate;}
      public void setModifDate(Date modifDate) {this.modifDate= modifDate;}
      /* ... */
  }
  </code></pre>
  
  <p>The class <code>CATPK</code> represents a primary key of an instance of <code>CAT</code> :</p>
  
  <pre><code>@Embeddable
  public class CATPKimplements Serializable {
      //default serial version id, required for serializable classes.
      private static final long serialVersionUID = 1L;
      @Column(name="ID_CAT")
      private String idCAT;
      @Column(name="ID_DEMANDE")
      private String idDemande;
  
      public CATPK() {}
      public String getIdCAT() {return this.idCAT;}
      public void setIdCAT(String idCAT) {this.idCat= idCat;}
      public String getIdDemande() {return this.idDemande;}
      public void setIdDemande(String idDemande) {this.idDemande = idDemande;}
      /* ...*/
  }
  </code></pre>
  
  <p>So basicly, the primay key is made of 2 different ID.</p>
  
  <p>Now, sometimes, before inserting a <code>CAT</code> in my database I checked if it is not already in the <code>C_A_T</code> table :</p>
  
  <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("cie");
  em = emf.createEntityManager();
  em.getTransaction().begin();
  // inCAT is an instance of CAT on which there is a complete primary key CATPK (made of 2 id)
  CAT CATinDB = em.find( CAT.class, inCAT.getId() );
  if (null == CATinDB)
    em.persist(inCAT); // CAT created
  em.getTransaction().commit();
  em.close();
  </code></pre>
  
  <p>Now, the problem is that the line <code>em.find( CAT.class, inCAT.getId() );</code> doesn't return <code>null</code> when it should.</p>
  
  <p>For instance, CATinDB can contain a row when I actually have no rows with the right <code>idCAT</code> and <code>idDemande</code>.</p>
  
  <p>So will <code>find</code> consider that it is found with only one id of the <code>catPK</code> matching ?</p>
  
  <p>In the end, I changed the line :</p>
  
  <pre><code>if (null == CATinDB)
  </code></pre>
  
  <p>into :</p>
  
  <pre><code>if (null == CATinDB || CATinDB.getId().getIdCAT() != inCAT.getId().getIdCAT() || CATinDB.getId().getIdDemande() != inCAT.getId().getIdDemande())
  </code></pre>
  
  <p>Not really sure why <code>find</code> wasn't returning null when I had no records matching the 2 id.</p>