Enterprise Java Development@TOPIC@

Chapter 60. Foreign Keys

60.1. Primary Key Join
60.1.1. @PrimaryKeyJoin Annotation
60.2. Using Composite Primary Key Property as Foreign Key
60.2.1. Using @IdClass Composite Primary Key Property as Foreign Key
60.2.2. Using @EmbeddedId Composite Primary Key Property as Foreign Key
60.3. Using Foreign Key in Composite Primary Key
60.3.1. Using Foreign Key in @IdClass Composite Primary Key
60.3.2. Using Foreign Key in @EmbeddedId Composite Primary Key (@MapsId)
60.4. Join Tables
60.4.1. @JoinTable Annotation
60.5. Summary



Note

In this case, the primary key is used as the foreign key and must be set. To use the foreign key as the primary key -- use @MapsId

    //@PrimaryKeyJoinColumn  //the two tables will be joined by PKs
    @MapsId
    private Person identity;
    public Borrower(Person identity) {
        this.identity = identity;
    }


  • Setting primary key prior to persisting

  • Foreign key used twice -- once for relation and once for primary key




  • HOUSE_ID is both primary and foreign key for ROOM









  • No longer modeling separate primary key - derived from foreign key











  • Separate table created to hold foreign keys

  • Can be used for all relationship enumatations and directions


  • Join table name either derived from associated tables or explicitly named

  • Join table columns either derived from referenced table column or explicitly named


  • Unique constraint enforces the (1)-to-Many aspect of relationship