The ORM allows to have a framework for storing objects within a relational databases and translating between both to organize and store data.
What is ORM?
— ORM is a technique for storing, retrieving, updating, and deleting Object-oriented program OOP from a relational database.
— The system use a “Data layer” which exist between the model/class and the Database to manage the translation between this two.
— -The data layer usually is a library written in a OO languages such as Java or C#.
Example
In the above example left side we have an object or class of a car with properties like model and price which can be string or integer. On the right side, we have a database with columns with the same name of the properties of that object, and in each row, we are going to list all the value of that object, and this is how our data get stored in a database.
There are some operations that we need to be able to perform in a way to work with a database, usually, we need to be able to take the object we created and store it in the database, get that object to delete it or edit.
In the above example, we are going to cover the primary operations, save and read an object from a database.
Our Data Layer is going to be in the middle between our rational database and out OO programming and it is the one that is going to handle the object to relational mapping for us.
Save data
If I want to save my car object in the database the data layer will give me a method to call save passing the object into the data layer and ask it to save it.
On the other side, the data layer knows what does that mean in term of a rational database and will insert new rows with the equal command into a table and will know which table we wanted based on the object/ class and knowing which properties of that object go in which columns.
Read data
If we want to get data out of the database, we will ask for some information giving certain criteria through the data layer, so we can select a given row from our car table. The data layer will then create a new car object and populate the properties automatically for us.
One to many relationships
This gets a bit more complicated when we work with the relationship between objects and relationships between data and different tables.
Let’s say that we want to introduce a new property, “category”, in our object, and we have a new class of categories where we want to be able to insert multiple cars, in the way that each car object created will have its own category.
In the above example, we have two cars that have the same category, and instead of having two different objects they both point/reference the same object on the right side. This is how these two objects are related in an object-relational way within our OOP.
How to store this relation in our database ??
Each table in our database will have a primary key /unique id. to associate this two tables in the cars database will add an extra column, category, and will give it a foreign key which will be equal to the ID/primary key of the category table. This will allow us to store this relationship between car objects and category objects. The database will be able to reference properly these objects for us when we are getting them out from the data layer.