Showing posts with label Identity Map. Show all posts
Showing posts with label Identity Map. Show all posts

Saturday, 14 September 2013

Identity Map 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 Identity Map Design Pattern?

The Identity Map Pattern is a core Object-Relational Behavioral Pattern introduced in Martin Fowler’s classic book, Patterns of Enterprise Application Architecture (PofEAA).

Core Definition: An Identity Map ensures that each object gets loaded only once by keeping every loaded object in a map. When you look for an object, you check the map first to see if it is already loaded.

It acts as an in-memory record registry that maps each database primary key to its corresponding loaded in-memory domain instance during a business transaction.

The Problem: Duplicate In-Memory Objects

Without an Identity Map, loading the same database record multiple times within the same business transaction causes serious issues:

  • Repeated Database Round-trips: Every lookup triggers a redundant SQL SELECT query, wasting database resources.
  • Equivalence Problems: Querying the same row twice creates two separate Java instances in memory. Checking person1 == person2 evaluates to false even though both objects represent the exact same database record.
  • Inconsistent State & Lost Updates: If one part of your code updates person1 and another part modifies person2, one update overwrites the other when written back to the database, resulting in data corruption.

How the Identity Map Works

Whenever a business method or data mapper requests an entity by its ID (e.g., findPerson(101)):

  1. Check Map: The mapper inspects the Identity Map using the ID as the key.
  2. Cache Hit: If the object already exists in the map, it returns that exact cached instance immediately without touching the database.
  3. Cache Miss: If not found, the mapper executes an SQL query, constructs the domain object, registers it in the Identity Map, and returns it.
public class PersonIdentityMap {
    private static Map<Long, Person> knownPersons = new HashMap<>();

    public static Person getPerson(Long id) {
        return knownPersons.get(id);
    }

    public static void addPerson(Person person) {
        knownPersons.put(person.getId(), person);
    }
}

Key Advantages of Using an Identity Map

  • Guarantees Single In-Memory Instance: Ensures that all parts of a transaction share the exact same reference to an entity.
  • Eliminates Redundant SQL Queries: Acts as a first-level transactional cache, boosting application response times.
  • Foundation of Modern ORM Frameworks: Forms the core mechanism behind the First-Level Cache (Session cache) in Hibernate and the JPA EntityManager.

Recommended Design Pattern Tutorials:


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

Click the below Image to Enlarge
Identity Map Design Pattern














See also:

  • All Design Patterns Links
  • Tutorials