Can someone explain, in layman terms, what is Object/relational mapping(ORM) in relation to Hibernate and JDBC?
Diagrams would be especially helpful for understandi
ORM allows you to use java objects as representation of a relational database. It maps the two concepts (object-oriented and relational)
Hibernate is an ORM framework - you describe how your objects are represented in your database, and hibernate handles the conversion.
JDBC is the API for database access, and it works "in a relational way" - you query tables and get rows and columns back. Hibernate uses JDBC under the hood to fetch the data and later convert it to objects.
A jdbc ResultSet
has multiple records, and each record has a set of columns. In hibernate this becomes of List<SomeClass>
where SomeClass has a field for every column in the database table, and there is one instance of
SomeClass` per database record.
Going by your diagram, we have a Person class which has data members : Login, Surname, FirstName and Address. Now Address for each Person object is an object of Address class.
Now look at the database tables. We have one Person table corresponding to the Person class and an Address table for Address class. Now there is a relationship between these 2 tables. The primary key of the address table (ID) is mapped to the foreign key (addr) of the Person table. This way a relation is established between the two tables.
But for our Person and Address classes we have no such relationship. So what we do is treat the address as a separate object and then integrate with the Person class. So we are actually compromising with the object-oriented methodology to match the relational method of the table. This is actually a very bad way to solve this problem as there's a huge gap between how we are handling the data in the tables and in the classes using object.
This problem is addressed in what is called as Object Relational Mapping (ORM) where we match the two concepts of Object-oriented and relational. ORM allows you to use java objects as representation of a relational database.
Hibernate is an ORM framework - you describe how your objects are represented in your database, and hibernate handles the conversion.
I was reading on Hibernate and stumbled across this thread. Doing further research, I found this other great explanation which may help someone:
Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.
An ORM tool simplifies the data creation, data manipulation and data access. It is a programming technique that maps the object to the data stored in the database.
Advantages of Hibernate:
1) Opensource and Lightweight: Hibernate framework is opensource under the LGPL license and lightweight.
2) Fast performance: The performance of hibernate framework is fast because cache is internally used in hibernate framework. There are two types of cache in hibernate framework first level cache and second level cache. First level cache is enabled bydefault.
3) Database Independent query: HQL (Hibernate Query Language) is the object-oriented version of SQL. It generates the database independent queries. So you don't need to write database specific queries. Before Hibernate, If database is changed for the project, we need to change the SQL query as well that leads to the maintenance problem.
4) Automatic table creation: Hibernate framework provides the facility to create the tables of the database automatically. So there is no need to create tables in the database manually.
5) Simplifies complex join: To fetch data form multiple tables is easy in hibernate framework.
6) Provides query statistics and database status: Hibernate supports Query cache and provide statistics about query and database status.
Information from javatpoint