Get to know object-relational mapping (ORM) frameworks, which automatically map table rows to programming language objects. Learn about the basic CRUD (create, read, update, delete) operations needed for persistence, as well as different mapping patterns based on repositories and services or an active record. Implement multiple applications, such as a task tracker, Mario Kart, and a device management platform, using the ORM of your choice.
Examples
Exercises
Context
As we have learned so far, relational databases use tables and rows to store data. The level of Data Normalization dictates which tables are set up and how records are associated.
Applications, on the other side, use rich in memory data structures like nested objects to structure code which can be loaded from and saved into rows in tables. The mismatch between both worlds is called the impedance mismatch.
The minimal operations required to support full persistence with a database are called CRUD operations. CRUD is an acronym for the four verbs used to operate on data:
Create | Insert a new row with attributes into a table INSERT INTO ... . |
---|---|
Read | Find a single row by attributes and map attribute values to object SELECT * FROM ... LIMIT 1 and find multiple rows by attributes, map attribute values to list of objects SELECT * FROM ... . |
Update | Update a row with changed attribute values UPDATE table SET ... WHERE ... . |
Delete | Delete a table rowDELETE FROM table WHERE ... . |
To overcome the impedance mismatch, objects need to be mapped to relations (tables) and vice versa. Typically, this task is automated by object-relational mapping frameworks, which are also called ORMs. ORMs are available in almost any programming language and follow similar patterns we will get to know in the subsequent sections.
A model or entity defines an object. An instance of this model is mapped to a database row. Depending on the ORM framework, the model either defines all attributes and associations or only the associations.
Let’s have a look at some examples, where we implement a Musician
having multiple Albums
with different ORM frameworks.