title: OneToOne relationship for a non primary key column
body: <p>I'm having a problem when I query an entity who has a OneToOne relationship with another one.  This is the scenario:</p>
  
  <p><strong>Database tables:</strong></p>
  
  <pre><code>create table people (
      id decimal(10,0) NOT NULL,
      email varchar(512) NOT NULL
  );
  
  create table users (
      email varchar(512) NOT NULL
  );
  </code></pre>
  
  <p><strong>Test data:</strong></p>
  
  <pre><code>insert into users (email) values ('jhon@domain.com');
  insert into users (email) values ('mary@domain.com');
  
  insert into people (id, email) values (1, 'jhon@domain.com');
  insert into people (id, email) values (2, 'mary@domain.com');
  </code></pre>
  
  <p><strong>Entities:</strong></p>
  
  <pre><code>@Entity(name = "people")
  public class Person implements Serializable {
  
      @Column
      @Id
      private long id;
  
      @Column
      private String email;
  
      public long getId() {
          return id;
      }
  
      public void setId(long id) {
          this.id = id;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
  }
  
  @Entity(name = "tbl_users")
  public class User implements Serializable {
  
      @Id
      private String email;
  
      @OneToOne(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
      @JoinColumn(name = "email", referencedColumnName = "email")
      private Person person;
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  }
  </code></pre>
  
  <p><strong>Invocation:</strong></p>
  
  <pre><code>...
  User user = entityManager.find(User.class, "jhon@domain.com");
  ...
  </code></pre>
  
  <p>After de invocation, the hibernate's logs shows:</p>
  
  <pre><code>select user1_.email as email2_0_, person2_.id as id1_1_, person2_.email as email1_1_
  from users user1_ left outer join people person2_ on user1_.email=person2_.id
  where user1_.email=?
  </code></pre>
  
  <p>As you can see, the join is wrong because is comparing users.email with people.id (<strong>user1_.email=person2_.id</strong>), so it returns an <strong>User</strong> without its corresponding <strong>Person</strong>.</p>
  
  <p>Any ideas about how can I fix it?</p>
  
  <p>Thanks a lot !!</p>