title: Wrong parameter binding when using named queries with optional parameters in EclipseLink
body: <p>I'm trying to do a database lookup using JPA with EclipseLink. My database is Oracle 11.2.0. I have the following entity classes defined:</p>
  
  <pre><code>@Entity
  @Table(name = "BS_PROVIDERS")
  public class BsProvider {
  
      @Id
      @Column(name = "GUID")
      private String guid;
      public String getGuid() { return guid; }
      public void setGuid(String guid) { this.guid = guid; }
  
      @Column(name = "NAME")
      private String name;
      public String getName() { return name; }
      public void setName(String name) { this.name = name; }
  
  }
  
  @Entity
  @Table(name = "CP_PROVIDERS")
  public class CpProvider{
  
      @Id
      @Column(name = "GUID")
      private String guid;
      public String getGuid() { return guid; }
      public void setGuid(String guid) { this.guid = guid; }
  
      @Column(name = "NAME")
      private String name;
      public String getName() { return name; }
      public void setName(String name) { this.name = name; }
  
  }
  
  @NamedQueries({
      @NamedQuery(
          name = Catalog.FIND_BY_CPP_AND_BSP,
          query = "select c from Catalog c where (c.cpProvider = :cpProvider) and ( (:bsProvider IS NULL) or (c.bsProvider = :bsProvider))"
          )
  })
  @Entity
  @Table(name = "CATALOGS")
  public class Catalog {
  
      public static final String FIND_BY_CPP_AND_BSP = "Catalog.findByCppAndBsp";
      public static final String CP_PROVIDER_PARAM = "cpProvider";
      public static final String BS_PROVIDER_PARAM = "bsProvider";
  
      @Id
      @Column(name = "GUID")
      private String guid;
      public String getGuid() { return guid; }
      public void setGuid(String guid) { this.guid = guid; }
  
      @Column(name = "NAME")
      private String name;
      public String getName() { return name; }
      public void setName() { this.name = name; }
  
      @ManyToOne
      @JoinColumn(name = "CPP_GUID")
      private CpProvider cpProvider;
      public CpProvider getCpProvider() { return cpProvider; }
      public void setCpProvider(CpProvider cpProvider) { this.cpProvider = cpProvider; }
  
      @ManyToOne()
      @JoinColumn(name = "BSP_GUIG")
      private BsProvider bsProvider;
      public BsProvider getBsProvider() { return bsProvider; }
      public void setBsProvider(BsProvider bsProvider) { this.bsProvider = bsProvider; }
  
  }
  </code></pre>
  
  <p>Code that creates the query and sets the parameters:</p>
  
  <pre><code>TypedQuery&lt;Catalog&gt; catalogQuery = em.createNamedQuery(Catalog.FIND_BY_CPP_AND_BSP, Catalog.class);
  catalogQuery.setParameter(Catalog.CP_PROVIDER_PARAM, cpProvider);
  catalogQuery.setParameter(Catalog.BS_PROVIDER_PARAM, bsProvider);
  List&lt;Catalog&gt; catalogList = catalogQuery.getResultList();
  </code></pre>
  
  <p>When the variable bsProvider is NULL, all parameters are registerd are registerd correctly according to the EclipseLink log:</p>
  
  <pre><code>    SELECT GUID, NAME, BSP_GUIG, CPP_GUID FROM CATALOGS WHERE ((CPP_GUID = ?) AND ((? IS NULL) OR (BSP_GUIG = ?)))
          bind =&gt; [18EC0EDB-21A4-4845-960A-D5D2BDAC7B87, null, null]
  </code></pre>
  
  <p>Otherwise when the variable bsProvider refers to an existing entity I get the following exception:</p>
  
  <pre><code>Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
  Internal Exception: java.sql.SQLException: Invalid column type
  Error Code: 17004
  Call: SELECT GUID, NAME, BSP_GUIG, CPP_GUID FROM CATALOGS WHERE ((CPP_GUID = ?) AND ((? IS NULL) OR (BSP_GUIG = ?)))
      bind =&gt; [18EC0EDB-21A4-4845-960A-D5D2BDAC7B87, com.bssys.ebpp.dbaccess.model.BsProvider@5dbbe8df, 44E8F4BF-CFDC-49DB-AB0B-718C72D6B4EF]
  </code></pre>
  
  <p>As you can see the first and the third parameters are bound correctly (they are replaced by the primary key values), but the second is not. What's the reason of such a strange behavior?</p>