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




