Understanding about Object Relational Mapping

Anyone who wants to learn about JPA needs to know about the concept of Object Relational Mapping. So what is it actually?

Suppose, you are working on an application and your application uses a table named users in the database. The structure of this table is as follows:

When we learned about JDBC, in order to get all the data in this users table, we had to write the code to connect to the database and use the SELECT statement as below:

Then, you have to browse each line of the results to get the data you want.

In addition to the different database types, such as MySQL, Oracle, … have the same query purpose but the syntax is completely different depending on the type of database.

But now we do not have to be so hard anymore!

Object Relational Mapping is a programming technique that allows us to manipulate databases via Java objects. We will map the tables and columns of our database table to its objects and attributes. The relationship between the tables and columns is also reflected in the objects to which it is mapped.

As you can see in the example above, we can map the users table to an object named Users. As follows:

As you can see, the id column in the users table is mapped to the id attribute in the Users object, which is the same for the other columns.

And now you can use the Users object above to manipulate the database without regard to the type of database which you are using. That someone else worried, the JDBC Driver 😀

Add Comment