liveBook · Manning

Join table

As explained in Java Persistence with Spring Data and Hibernate epub

A join table is a relational table that stores key-based links between rows of associated entity tables, commonly for a many-to-many association.

A join table stores links between rows in other tables. It is the usual relational representation of a many-to-many association: each row holds keys for one pair of entities. The table need not appear as a class in the domain model. section 1.2.4 section 9.3.1

CREATE TABLE USER_BILLINGDETAILS (
    USER_ID BIGINT,
    BILLINGDETAILS_ID BIGINT,
    PRIMARY KEY (USER_ID, BILLINGDETAILS_ID),
    FOREIGN KEY (USER_ID) REFERENCES USERS(ID),
    FOREIGN KEY (BILLINGDETAILS_ID) REFERENCES BILLINGDETAILS(ID)
);

USER_BILLINGDETAILS shows the basic shape: two foreign keys form a composite primary key, so each user–billing-details pair can be linked once. section 1.2.4

A join table is not limited to many-to-many links. For an optional association, a missing link is represented by no join-table row rather than a nullable foreign key on an entity table. Keys and uniqueness constraints then enforce the intended multiplicity. section 9.1.4 section 9.2.3

Choose constraints and mappings to match the association:

  • Optional one-to-one: ITEM_SHIPMENT uses SHIPMENT_ID as its primary key and makes ITEM_ID unique. A shipment without an item has no link row. A JPA @OneToOne can specify the table and both join columns with @JoinTable. section 9.1.4
  • Optional one-to-many: ITEM_BUYER gives each item at most one buyer, while a buyer can be linked to many items. Map the Item.buyer association with @ManyToOne and @JoinTable; User.boughtItems can be the mapped-by side. section 9.2.3
  • Many-to-many: CATEGORY_ITEM has foreign keys to CATEGORY and ITEM and a composite primary key. Map the owning collection with @ManyToMany and @JoinTable; use mappedBy for an optional inverse collection. section 9.3.1

An embeddable class can also map an optional one-to-many association through @JoinTable; without @JoinTable or @JoinColumn, that mapping defaults to a join table. For more complex links, a table can hold three keys: CATEGORY_ITEM can connect a category, an item, and the user who added it through a Map<Item, User>. That compact API is less accommodating if the link later needs attributes such as a timestamp. An intermediate entity or component is another option for representing many-to-many links. section 9.2.4 section 9.4.2 section 9.5

Use a collection mapping when the application needs to navigate or iterate the associated entities, not merely because a join table exists. Collection mappings can simplify access while complicating storage, updates, and deletion. section 9.5

How Java Persistence with Spring Data and Hibernate epub covers it

How deep it goes
Works through keys, uniqueness constraints, and JPA mappings for several multiplicities, while leaving broader collection-mapping tradeoffs as guidance.
Examples
Uses user–billing-details SQL, shipment–item and buyer–item mappings, category–item collections, and a three-key Map association.
What it assumes
Assumes working familiarity with Java entities and collections, SQL keys, and JPA association annotations.
What you can do after
Apply it via JPA @JoinTable and SQL constraints to map links with the intended optionality and multiplicity.
How it connects
Connects foreign keys, composite primary keys, uniqueness constraints, optional associations, many-to-many mappings, embeddable classes, and intermediate entities.

Also explained in

Compare how 5 books explain join table →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Java Persistence with Spring Data and Hibernate epub. The section links point to the parts of the book it draws on.