title: "org.hibernate.TypeMismatchException: Provided id of the wrong type" when using @OneToOne for a bi-directional relation
body: <p>I don't succeed in using @OneToOne when the composite primary key is the same in both classes:</p>
  
  <pre><code>@Entity
  public class One implements Serializable {
      private static final long serialVersionUID = 1L;
      @EmbeddedId
      private OnePK onePK;
      @OneToOne(cascade = CascadeType.ALL, mappedBy = "one")
      private Two two;
      //... constructors, getters and setters
  }
  
  @Entity
  public class Two implements Serializable {
      private static final long serialVersionUID = 1L;
      @EmbeddedId
      private TwoPK twoPK;
      @JoinColumns({
          @JoinColumn(name = "P_ID", referencedColumnName = "P_ID", insertable = false, updatable = false),
          @JoinColumn(name = "L_ID", referencedColumnName = "L_ID", insertable = false, updatable = false)})
      @OneToOne(optional = false)
      private One one;
      //... constructors, getters and setters
  }
  
  @Embeddable
  public class OnePK implements Serializable {
      @Basic(optional = false)
      @Column(name = "P_ID")
      private BigInteger dId;
      @Basic(optional = false)
      @Column(name = "L_ID")
      private BigInteger lId;
      //... constructors, getters and setters
  }
  
  @Embeddable
  public class TwoPK implements Serializable {
      @Basic(optional = false)
      @Column(name = "P_ID")
      private BigInteger dId;
      @Basic(optional = false)
      @Column(name = "L_ID")
      private BigInteger lId;
      //... constructors, getters and setters
  }
  </code></pre>
  
  <p>When I use the source code above as is, I get:</p>
  
  <blockquote>
    <p>12:56:04,021 WARNING [org.apache.cxf.phase.PhaseInterceptorChain]
    (default task-83) Application
    {<a href="http://services.webservices" rel="nofollow noreferrer">http://services.webservices</a>.*****.******.com/}ObjectManagerService#{<a href="http://services.webservices" rel="nofollow noreferrer">http://services.webservices</a>.*****.******.com/}getAllObjects
    has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault:
    java.lang.IllegalArgumentException:
    org.hibernate.TypeMismatchException: Provided id of the wrong type for
    class com.******.*****.<strong>.********.Two. Expected: class
    com.******.*****.</strong>.********.TwoPK, got class
    com.******.*****.**.********.OnePK    at
    org.apache.cxf.service.invoker.AbstractInvoker.createFault(AbstractInvoker.java:162)
      at
    org.apache.cxf.jaxws.AbstractJAXWSMethodInvoker.createFault(AbstractJAXWSMethodInvoker.java:267)
      at</p>
  </blockquote>
  
  <p>When I use the same class (OnePK) as the primary key of both classes (One and Two), I get:</p>
  
  <blockquote>
    <p>11:44:35,735 WARNING [org.apache.cxf.phase.PhaseInterceptorChain]
    (default task-83) Interceptor for
    {<a href="http://services.webservices" rel="nofollow noreferrer">http://services.webservices</a>.*****.******.com/}ObjectManagerService#{<a href="http://services.webservices" rel="nofollow noreferrer">http://services.webservices</a>.*****.******.com/}getAllObjects
    has thrown exception, unwinding now: org.apache.cxf.interceptor.Fault:
    Marshalling Error: A cycle is detected in the object graph. This will
    cause infinitely deep XML : One[ onePK=OnePK[ dId=3151, lId=426 ] ] ->
    Two[ twoPK=OnePK[ dId=3151, lId=426 ] ] -> One[ onePK=OnePK[ dId=3151,
    lId=426 ] ]   at
    org.apache.cxf.jaxb.JAXBEncoderDecoder.marshall(JAXBEncoderDecoder.java:266)
      at
    org.apache.cxf.jaxb.io.DataWriterImpl.write(DataWriterImpl.java:238)
      at
    org.apache.cxf.interceptor.AbstractOutDatabindingInterceptor.writeParts(AbstractOutDatabindingInterceptor.java:118)
      at</p>
  </blockquote>
  
  <p>Using @PrimaryKeyJoinColumns didn't help, I used <a href="https://stackoverflow.com/a/10805591/458157">this answer</a> and <a href="http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example-annotation/" rel="nofollow noreferrer">this tutorial</a>, I got the same error messages. Using @MapsId didn't help, I got "org.hibernate.exception.GenericJDBCException: could not prepare statement". Maybe the root cause is <a href="https://hibernate.atlassian.net/browse/HHH-3993" rel="nofollow noreferrer">this unfixed bug of Hibernate</a>. The only workaround that I've found consists in removing the very first occurrence of @OneToOne and the field "two" in the class "One" but the instance of Two contained by an instance of One no longer gets deleted when I delete this instance of One, that's why I used CascadeType.ALL.</p>
  
  <p>This JPA source code is auto-generated by Netbeans 8. I'm a bit out of options. Is there a way of making it work correctly? Is it really a limitation of Hibernate? Is there another implementation of JPA handling this case correctly?</p>