Showing posts with label Dependent Mapping. Show all posts
Showing posts with label Dependent Mapping. Show all posts

Friday, 9 August 2013

Dependent Mapping Design Pattern

🚀 Master Enterprise Java & Architecture Patterns!

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

🔔 Subscribe Now

What is the Dependent Mapping Design Pattern?

The Dependent Mapping Pattern is one of the essential Object-Relational Structural Patterns defined in Martin Fowler’s Patterns of Enterprise Application Architecture (PofEAA).

Core Definition: In the Dependent Mapping pattern, the parent class performs all database mapping operations on behalf of a child (dependent) class.

This pattern serves as a practical, lightweight alternative to standard Foreign Key Mapping when an object cannot exist independently of its parent object.

Key Principles of Dependent Mapping

  • Parent Controls Persistence: The dependent class does not have its own independent database mapper or repository. Instead, the parent entity orchestrates loading, updating, and saving child records.
  • No Independent Identity: The dependent object is strictly scoped to its parent lifecycle—if the parent is deleted or queried, the child follows automatically.
  • Simpler Domain Models: In simple domain architectures, placing database mapping logic directly in the parent simplifies the overall system and reduces unnecessary boilerplate classes.

Real-World Example: Person and Address

Consider two database tables: a Person table and an Address table.

  • Person Table: Columns are person_id (Primary Key), name, age, and gender.
  • Address Table: Columns are address_id (Primary Key), person_id (Foreign Key), and city.

A person named Joe may have three addresses (Bangalore, Chennai, Delhi), and Goni may have two addresses (Mumbai, Kerala). Rather than managing addresses through an independent address service, the Person class handles the mapping:

public class Person {
    private Long personId;
    private String name;
    private int age;
    private String gender;
    private Set<Address> addresses = new HashSet<>();

    // Loads dependent address data from the address table
    public void loadAddress() {
        // Query Address table where person_id = this.personId
        // Populate the addresses collection
    }

    // Saves dependent address records alongside person data
    public void saveAddress() {
        // Persist all addresses referencing this.personId
    }
}

When Should You Use Dependent Mapping?

  • When child objects are tightly coupled to a single parent and are never referenced by other entities.
  • When you want to treat child objects as direct value objects or dependent components without building separate data access infrastructure.
  • When you want to simplify transaction and cascade management directly within the parent domain boundary.

Recommended Design Pattern Tutorials:


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

Click the below Image to Enlarge
Dependent Mapping Design Pattern















See also:

  • All Design Patterns Links
  • Tutorials