title: Hibernate @OneToOne relationship with multiple objects?
body: <p>If I have a table like this...</p>
  
  <pre><code>+------------------------------------------------------+
  |                         Code                         |
  +------------------------------------------------------+
  | id |               target_uuid              |  code  |
  +----+----------------------------------------+--------+
  |  1 | "01BE898A-C5A9-4F86-B0AA-4ACCDCE81B8F" |    0   |
  +----+----------------------------------------+--------+
  |  2 | "E139F21E-8C93-492B-9E0F-CC773FAE832D" |    2   |
  +----+----------------------------------------+--------+
  </code></pre>
  
  <p>...and the <code>target_uuid</code> could refer to a <code>Post</code> or <code>User</code> uuid field, how would I map this in Hibernate?</p>
  
  <p>Assume <code>uuid</code> is not the primary key for <code>Post</code> or <code>User</code> here. I know, I know. Bad data model, but I'm stuck with it.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>@Entity
  @Table(name = "User")
  public class User {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Integer id;
  
      @Column(name = "uuid")
      private String uuid;
  
      @OneToOne(mappedBy = "user")
      @JoinColumn(name = "uuid")
      private Code code;
  
      // getters and setters
  }
  
  @Entity
  @Table(name = "Post")
  public class Post {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Integer id;
  
      @Column(name = "uuid")
      private String uuid;
  
      @OneToOne(mappedBy = "post")
      @JoinColumn(name = "uuid")
      private Code code;
  
      // getters and setters
  }
  
  @Entity
  @Table(name = "Code")
  public class Code {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      @Column(name = "id")
      private Integer id;
  
      @Column(name = "code")
      private Integer code;
  
      @Column(name = "target_uuid")
      private String targetUuid;
  
      @OneToOne
      @JoinColumn(name = "target_uuid", insertable = false, updatable = false)
      private User user;
  
      @OneToOne
      @JoinColumn(name = "target_uuid", insertable = false, updatable = false)
      private Post post;
  
      // getters and setters
  }
  </code></pre>
  
  <p>This does not throw an exception and seems to work, but my <code>User</code> and <code>Post</code> objects keep getting NULL values for their <code>code</code> properties.</p>
  
  <p>Can anyone shed some light on this?</p>