title: Shared field in composite primary key for foreign keys
body: <p>I have 4 tables in my database:</p>
  
  <pre><code>+-------------+
  |Libraries    |
  +-------------+ . . . . . _________
  |P code       |                   |
  +-------------+                   -
        .                           ^
        .                  +---------------+
        |                  |Persons        |
        |                  +---------------+
        -                  |P  name        |
        ^                  |FP library_code|
  +-----------------+      +---------------+
  |Books            |              .
  +-----------------+              .
  |P   title        |              |
  |FP  library_code |              |
  +-----------------+              -
        .                          ^
        .                 +----------------+  
        |                 |Borrows         |
        +_______________|&lt;+----------------+
                          |FP person_name  |
                          |FP book_title   |
                          |FP library_code |
                          +----------------+
  
  P - primary key
  FP - foreign primary key
  
  _
  ^ - foreign key is part of primary key
  </code></pre>
  
  <p>For <code>Persons</code> and <code>Books</code> column <em>library_code</em> in <code>Borrows</code> should be the same (shared column)!</p>
  
  <p>My java code looks like:</p>
  
  <pre><code>@Entity
  class Library {
      @Id
      private String code;
  }
  
  @Embeddable
  class PersonId implements Serializable {
      private String name;
      private String library_code;
  }
  
  @Entity 
  class Person {
      @EmbeddedId 
      private PersonId id;
  }
  
  @Embeddable
  class BookId implements Serializable {
      private String title;
      private String library_code;
  }
  
  @Entity 
  class Book {
      @EmbeddedId 
      private BookId id;
  }
  </code></pre>
  
  <p><strong>Is it possible to implement <code>Borrow</code> entity like this with the shared <em>library_code</em> column for <code>PersonId</code> and <code>BookId</code>, how?</strong>:</p>
  
  <pre><code>@Embeddable
  class BorrowId implements Serializable {
      private PersonId person;
      private BookId book;
  }
  
  @Entity 
  class Borrow {
      @EmbeddedId 
      private BorrowId id;
  }
  </code></pre>
  
  <p>Note: this question is not about database model, the model is already provided and I can't change it, I just need find the best way how to map it to entity model.</p>