Mapping Relational DB Flashcards
(25 cards)
Why is storing objects in relational databases a problem?
Objects in memory have a different structure compared to relational databases, objects are structured in hierarchies while relational databases store data in tables with rows and columns
Alternatives to relational databases
Object DBs, they store objects directly removing mismatches, but it’s risky
List the RDB mapping architectural patterns
1- Row Data Gateway
2- Table Data Gateway
3- Active Record
4- Data Mapper
What is a Gateway Pattern and how does it work?
A basic pattern for Row data gateway and table data gateway
1- It maps in-memory objects to data base tables
2- Each column in the table corresponds to a field in the class
3- The gateway contains all the mapping code but does not include the actual business logic
When do we use Row Data Gateway and when do we use Table data gateway
If the technology platform supports data recovery pattern Record Set then its easier to use Table data gateway, otherwise its easier to use row data gateway (an object for each record in a data source)
What is the Active Record pattern?
Appropriate when the domain model is similar to the database schema
It combines the gateway and the domain object in one class which include access to the DB and the business logic code
Disadvantage of an Active Record Pattern
it’s advisable to keep these responsibilities separate
What is Data Mapper Pattern?
A more complex but more flexible pattern
Advantage of a Data Mapper Pattern
The domain objects are independent from the database schema
Disadvantage of Data Mapper Pattern
Added complexity (for complex mappings, its possible to reuse existing object-relational mapping tools)
What is concurrency
It occurs when multiple users/processes try to modify the same data simultaneously, leading to conflicts
How does the Unit of work pattern help managing database changes
1- Tracking loaded objects to monitor changes
2- Batching updates to avoid unnecessary database writes
3- Ensuring consistency by saving all changes together in a single transaction
Without this pattern, the domain layer has to decide when to read and write
What is Data Reading
It uses mapping classes to encapsulate SQL queries for object-relational mapping
What does the identity map pattern do
It ensures that each database record is only read once and stored in memory (if unit of work is implemented, it maintains the identity map)
What are the challenges in mapping relational database data to domain model objects?
1-Links: in objects, relationships are represented using references (pointers to other objects) while in relational databases, relationships are managed using foreign keys
2- Collections: Objects can have collections (lists) but databases store only one value per field
3- Identity field: A special field in the object (primary key) to store the identity and help map the object to its relational DB keys
What happens when reading a foreign key in ORM?
If not in the Identity Map, load the object from the DB using the foreign key.
How are object references stored in the database?
References are replaced by the entity’s identity field (foreign key).
How is a collection read from the database?
By retrieving rows linked to the object’s identifier that holds the collection.
How is a collection stored in the database?
Each object in the collection is saved with a foreign key pointing to the owning object.
What are the three inheritance mechanisms?
1- Single Table Inheritance
2- Concrete Table Inheritance
3- Class Table Inheritance
Explain Single Table Inheritance
It uses a table to store all classes in a hierarchy, a lot of duplication and table affected by each domain change, but very
efficient, without JOINS.
Explain Concrete Table Inheritance
a table for each concrete class in the hierarchy, including inherited fields, some duplication and less flexible because domain changes affect many
tables, but more efficient, less JOINS.
Explain Class Table Inheritance
uses one table per class in the hierarchy, fields stored just in the table corresponding
to the class defining it, no duplication, very flexible/extensible, but inefficient, requires many JOINS.
What are Metadata files?
They show us the mapping process, they also facilitate the generation of the mapping code, commonly used in commercial tools