title: Spring Data JPA Non-key field references in Hibernate
body: <p>Is there any way in Hibernate that allows using non-primary key fields to describe relationships?</p>
  <p>How can I describe Users and Authorities in Hibernate?</p>
  <p>When I use <code>@OneToMany</code> and <code>@ManyToOne</code>, it just helps <code>Authorities</code> create a field called <code>user_id</code> and it will reference the User id.</p>
  <p><strong>Entity Relationship Model</strong></p>
  <p><a href="https://i.stack.imgur.com/1I5WY.png" rel="nofollow noreferrer">https://i.stack.imgur.com/3k0eS.png</a></p>
  <p><strong>Java classes</strong></p>
  <pre><code>@Entity
  @Table(name = &quot;Users&quot;)
  public class User {
  
      @Id
      @GeneratedValue
      private Long id;
  
      private String username;
  
      // ...getters and setters
  }
  </code></pre>
  <p>--</p>
  <pre><code>@Entity
  @Table(name = &quot;Authorities&quot;)
  public class Authority {
  
      @Id
      @GeneratedValue
      private Long id;
  
      @ManyToOne(fetch = FetchType.LAZY)
      private User user;
  
      // ...getters and setters
  }
  </code></pre>