Notes – ORM Tools in Hibernate
In Hibernate, ORM tools are the components and utilities that help developers map Java objects to database tables.
These tools manage how Hibernate interacts with the database โ from reading configurations to handling sessions and queries.
Core ORM Tools Provided by Hibernate
| Tool/Component | Purpose |
|---|---|
Configuration | Loads Hibernate settings from XML or annotation-based config files. |
SessionFactory | A factory for creating Session objects. Heavyweight, created once. |
Session | Interface between Java code and database. Used for CRUD operations. |
Transaction | Manages a single unit of work (commit/rollback). Ensures data integrity. |
Query / HQL | Executes queries on persistent objects using HQL or native SQL. |
Mapping Tools
Hibernate uses two main methods to define how Java classes map to database tables:
1. XML Mapping (Legacy Style)
- Mapping is defined in
.hbm.xmlfiles. - Still supported but less common today.
2. Annotation-Based Mapping (Modern Style)
- Uses JPA (Java Persistence API) annotations like
@Entity,@Table,@Column, etc. - Cleaner and more readable.
Configuration Files
hibernate.cfg.xml
Central configuration file that contains database connection settings and mapping information.hibernate.properties
Optional alternative for storing settings in a key-value format.
Utility Classes (Optional but Helpful)
HibernateUtil
A custom class used to create and manageSessionFactory.
Keeps code clean and avoids repetition.
public class HibernateUtil {
private static SessionFactory sessionFactory;
static {
// Configuration and sessionFactory initialization here
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
