title: How do I map a One To Many Relationship with Composite Primary Key in JPA (Hibernate)?
body: <p>My tables look like this:</p>
  <pre><code>School
  -------
  school_id (pk)
  ...
  
  Student
  school_id (pk) (fk)
  student_id (pk)
  ...
  </code></pre>
  <p>So using JPA (Hibernate), I tried something like this.</p>
  <pre><code>@Entity
  @Table(&quot;SCHOOL&quot;)
  public School {
      @Column(name = &quot;SCHOOL_ID&quot;)
      private String schoolId;
      
      @OneToMany
      private List&lt;Student&gt; students;
  }
  
  @Entity
  @Table(&quot;STUDENT&quot;)
  public Student {
      @EmbeddedId
      private StudentPK studentPK;
  
      @ManyToOne
      @JoinColumn(name = &quot;SCHOOL_ID&quot;)
      private School school; 
  }
  
  @Embeddable
  public StudentPK implements Serializable {
      @Column(name = &quot;SCHOOL_ID&quot;)
      private String schoolId;
  
      @Column(name = &quot;STUDENT_ID&quot;
      private String studentId;
  }
  
  </code></pre>
  <p>When I do this, I frequently get an error that says that the foreign key constraint is being violated on runtime. I suspect that JPA is trying to generate a new foreign key called &quot;SCHOOL_ID&quot; instead of using the existing column from my composite key, but I'm not sure how to force it to use the existing column.</p>