Data Type and Granularity Mismatches
You'll learn how object-oriented and relational data types clash, and why objects don't always map neatly to table columns.
While object identity, associations, and inheritance create mapping challenges, another fundamental mismatch occurs at the data type level. Objects and relational databases speak different "data languages."
The Data Type Problem
Relational databases support a limited set of primitive types: integers, strings, dates, decimals. Your object-oriented code, however, works with rich domain types like Money, PhoneNumber, Address, or DateRange. These domain objects encapsulate behavior and validation—but the database doesn't understand them.
Analogy: Imagine trying to mail a fully assembled piece of furniture (your rich object) through a postal system that only accepts flat envelopes (database columns). You must disassemble it first.
The Granularity Problem
Objects can be arbitrarily fine-grained or coarse-grained. A Customer object might contain an embedded Address object, which itself contains a Country object. Do you store these as separate tables (normalized), flatten them into the Customer table (denormalized), or use some hybrid approach?
The database forces you to choose between:
- Multiple tables: More joins, potential N+1 queries, but cleaner separation
- Single flattened table: Simpler queries but repeated data and loss of object structure
Collections add another dimension: an object might hold a list of values (List<String> emailAddresses), but databases don't natively support collection-valued columns without additional tables or serialization tricks.
Key Takeaway: The impedance mismatch extends beyond structure—rich object types must be serialized to database primitives, and object granularity (fine or coarse) must be mapped to rigid table structures, forcing design trade-offs between object fidelity and database efficiency.