Menu

What is ORM and Why Hibernate

What is ORM and Why Hibernate

What is Hibernate

Hibernate ORM, commonly called Hibernate, is an open-source ORM framework for Java. Gavin King created it and first released it in 2001. It is now maintained by Red Hat.

Hibernate maps Java classes to database tables and generates the SQL needed to save, load, update, and delete data. Developers work with objects, and Hibernate handles the database communication underneath.

Hibernate also implements Jakarta Persistence, the standard Java specification for ORM.

This handbook is based on Hibernate ORM 7.4, which is the current stable series at the time of writing.

The Problem Hibernate Solves

Java applications store data in objects, while most business data lives in relational databases as rows and tables. These two models are built differently, and moving data between them by hand takes a lot of code.

With plain JDBC, a developer opens a connection, writes SQL as strings, sets each parameter, reads each column from a result set, and copies the values into an object.

You have to repeat this for every operation on every table. The code is long, easy to get wrong, and hard to maintain when the schema changes.

What is ORM

ORM stands for Object-Relational Mapping. It connects an application's object model to a relational database's tables.

A Java class maps to a table. An object of that class maps to a row. The class fields map to the columns. The ORM tool reads these mappings and handles the conversion between objects and rows.

This need arises from differences between the two models, often called the object-relational impedance mismatch.

Area

Object model

Relational model

StructureClasses with fields and methodsTables with columns
InheritanceSupported directlyNot supported
IdentityCompared with == and equals()Compared by primary key
RelationshipsReferences between objectsForeign keys between tables
NavigationFollow references from one object to anotherJoin tables in a query

Why Use Hibernate

Hibernate reduces the amount of database code a developer must write and provides several built-in capabilities.

  • Less boilerplate. No manual result set handling or object copying.
  • Database independence. Hibernate uses a dialect for each database, so the same code can run on MySQL, PostgreSQL, Oracle, and others with little change.
  • Automatic change tracking. Hibernate detects which fields of a loaded object changed and updates only those.
  • Relationship handling. Hibernate maps and loads one-to-one, one-to-many, and many-to-many associations through object references.
  • Lazy loading. Related data can be fetched only when you access it.
  • Caching. Built-in caching reduces repeated database calls.
  • Query support. HQL, the Criteria API, and native SQL are all available.

Hibernate Tutorial

AP

Abhishek Pati



Learn via Video Course

Java Programming ( Telugu) Logo

Java Programming ( Telugu)

11 hrs



Get in Touch For Details