Clean • Professional
ORM (Object-Relational Mapping) is a programming technique that allows developers to interact with a relational database using objects and classes instead of writing raw SQL queries.
ORM acts as a bridge between object-oriented programming (OOP) and relational databases, automatically handling the conversion between Java objects and database tables.
ORM automatically maps:
This means developers focus on business logic, while ORM handles SQL generation and data mapping internally.
👉 In simple words:
ORM lets you work with the database using objects, not SQL.
Without ORM, developers must:
ResultSet into Java objectsORM automates all of this, making applications cleaner, safer, and easier to maintain.
Java Object
↓
ORM Framework (Hibernate)
↓
SQL Query
↓
Database
↓
Table Rows
↓
Java Objects
This example demonstrates how ORM maps a Java entity to a database table and eliminates the need for manual SQL and result mapping.
Entity Class (With ORM)
Defines a Java class as a database entity using JPA annotations, allowing ORM to handle table mapping automatically.
import jakarta.persistence.*;
@Entity
@Table(name = "users")
publicclassUser {
@Id
@GeneratedValue
private Long id;
private String name;
private String email;
}
Database Table
Represents how the ORM-mapped entity is stored in the relational database.
users
-----------------
id | name | email
Example Without ORM (JDBC)
Shows how data must be fetched and mapped manually when ORM is not used.
Stringsql="SELECT * FROM users WHERE id = ?";
PreparedStatementps= conn.prepareStatement(sql);
ps.setLong(1,1);
ResultSetrs= ps.executeQuery();
Useruser=newUser();
user.setId(rs.getLong("id"));
user.setName(rs.getString("name"));
Example With ORM (Spring Data JPA + Hibernate)
Demonstrates how ORM retrieves database records using repository methods without writing SQL.
Useruser= userRepository.findById(1L).get();
| Concept | Description |
|---|---|
| Entity | Java class that represents a database table |
| Mapping | Links entity fields to table columns |
| EntityManager / Session | Manages the lifecycle and persistence of entities |
| Relationships | Defines associations like One-to-One, One-to-Many, Many-to-One |
| Lazy Loading | Fetches related data only when it is accessed |
| Caching | Stores data temporarily to improve performance |
| Transactions | Ensures database operations are completed reliably and consistently |
Spring Boot integrates ORM using Spring Data JPA and Hibernate.
Spring Data JPA
↓
Hibernate (ORM Engine)
↓
Database
| Feature | ORM (Hibernate / JPA) | JDBC |
|---|---|---|
| SQL Writing | Very little SQL is needed; ORM generates queries automatically | Developers must write SQL manually |
| Data Mapping | Automatic mapping between objects and tables | Manual mapping using ResultSet and Java objects |
| Relationships | Easy to manage using annotations like @OneToMany | Complex and fully manual joins |
| Lazy Loading | Supported for optimized data fetching | Not supported |
| Maintainability | High, cleaner and more readable code | Low, more boilerplate and repetitive code |
| Performance | Medium, with some abstraction overhead | High when queries are carefully optimized |
Use ORM when:
Avoid ORM when:
E-commerce Application
User → Orders → OrderItems
This structure represents a typical real-world domain model with multiple related entities.
ORM automatically handles database operations such as joins, relationships, lazy loading, and cascading actions without requiring manual SQL.
ORM (Object-Relational Mapping) bridges the gap between object-oriented programming and relational databases.
It allows developers to:
In Spring Boot, Spring Data JPA + Hibernate is the most widely used ORM solution for enterprise-grade applications.