The **Identity Map** pattern ensures that each business object is loaded only once from the database in a single session.
It works as an internal session cache: when you request an object by its ID, the Identity Map first checks if it already has it in memory. If so, it returns the existing instance instead of querying the database again.
# Key Benefits
-
check_circle
Identity Consistency
Avoids having two different objects in memory representing the same database row.
-
check_circle
Performance
Drastically reduces repetitive database queries.
# Practical Example
if (map.containsKey(id)) {
return map.get(id); // Returns already loaded object
} else {
Person p = db.loadPerson(id);
map.put(id, p); // Saves it for next time
return p;
}