Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need an Object Relational Mapper?

Tags:

java

hibernate

I bought a book on Hibernate. I wanted to learn on what Hibernate is and what is Object Relational Mapping in general.

Harnessing Hibernate has good topics on Hibernate and I do think that I am able to write simple mapping classes now. My problem is, I think I am way ahead on the Hibernate but I really don't know why we need one.

The book explains Hibernate clearly but I think I am at lost because it does not discuss why we need Hibernate and an ORM.

like image 268
Mark Estrada Avatar asked Jan 01 '26 08:01

Mark Estrada


2 Answers

I too would suggest starting at wikipedia.

From there, follow the links at the bottom.

To provide a short answer: An ORM is used to abstract from the data storage, usually a database. This can serve multiple purposes, among those:

  • Application programmers can add and maintain functionality of the software without in-depth knowledge of the database (you can write your code in Java, not in SQL).
  • It takes away the pitfalls of having to assemble your SQL statements as strings and therefore eliminates a huge source of errors.
  • Database optimization is independent from business logic. This ensures better maintainability. The optimization can either be done by the ORM ( via configuration files) or directly in the database (by manually adding index tables and/or query caches). Both will not take place in the actual program code.
  • Data providers can more easily be exchanged, because the ORM usually offers drivers for multiple databases, so that the same functional logic will work with different products and vendors.
like image 72
weltraumpirat Avatar answered Jan 03 '26 21:01

weltraumpirat


At its core, it's about Separation of Concerns.

Data persistence and logic are two separate concerns. The application coder should not have to concern himself with what flavor of SQL the IT staff has decided to use this week, and how to translate between the different paradigms of relation entities and object hierarchy entities.

The DBA should also not need to concern himself with object ownership rules and other almost-leaky concerns from the business layer.

In reality, in larger systems, you rarely can be completely vendor-agnostic, and the DAO layer inevitably picks up some non-ORM-related queries for performance tweaks. Tools like Hibernate do the best they can to make these situations as rare and abstract as possible.

like image 30
codelark Avatar answered Jan 03 '26 21:01

codelark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!