Friday, 9 August 2013

Association Table Mapping Design Pattern

🚀 Master Enterprise Java & Architecture Patterns!

Subscribe to Ram N Java for clean, visual, and beginner-friendly programming tutorials!

🔔 Subscribe Now

What is the Association Table Mapping Design Pattern?

The Association Table Mapping Pattern is an essential Object-Relational Structural Pattern documented in Martin Fowler's classic guide, Patterns of Enterprise Application Architecture (PofEAA).

The Core Definition: It saves an association between two tables as a separate link table that contains foreign keys referencing each of the linked tables. This pattern is the standard architectural mechanism used to map Many-to-Many relationships between relational database tables and object-oriented domain models.

Real-World Example: Persons and Addresses (Many-to-Many)

Consider a scenario where multiple people can share multiple addresses:

  • Joe lives in both Bangalore and Chennai.
  • Goni lives in Bangalore, Chennai, and Kerala.
  • Notice that the city of Bangalore belongs to both Joe and Goni. This is a true Many-to-Many relationship.

Database Schema Structure: The 3 Tables

In a relational database, you cannot store multiple foreign keys in a single column without violating normalization rules. To solve this, we create three distinct tables:

  1. Person Table: Stores individual personal records (person_id, name, age).
  2. Address Table: Stores unique address locations (address_id, city).
  3. Person_Address Link Table (Association Table): Contains foreign keys pointing to both entities (person_id, address_id). Each row represents a single connection between a person and an address.

Mapping to In-Memory Java Classes

In object-oriented code, this link table is translated seamlessly into collections of references:

// Person entity containing a collection of Addresses
public class Person {
    private Long personId;
    private String name;
    private int age;
    private Set<Address> addresses = new HashSet<>();

    // Getters and Setters
}

// Address entity containing a collection of Persons
public class Address {
    private Long addressId;
    private String city;
    private Set<Person> persons = new HashSet<>();

    // Getters and Setters
}

When you load Joe, the data mapper reads the intermediate link table and populates Joe's addresses set with Bangalore and Chennai. Conversely, when you inspect Bangalore, its persons set contains both Joe and Goni.

Key Benefits of Association Table Mapping

  • Normalized Relational Schema: Keeps base tables cleanly decoupled without storing denormalized arrays or delimited strings in columns.
  • Bidirectional Traversal: Enables smooth navigation from either side of the relationship (Person to Address or Address to Person).
  • JPA/Hibernate Standard: Forms the exact architectural foundation used by the @ManyToMany and @JoinTable annotations in modern ORM frameworks.

Recommended Design Pattern Tutorials:


Click here to watch in Youtube : https://www.youtube.com/watch?v=YLcnIANJxz0

Click the below Image to Enlarge
Association Table Mapping Design Pattern















See also:

  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials