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/ComponentPurpose
ConfigurationLoads Hibernate settings from XML or annotation-based config files.
SessionFactoryA factory for creating Session objects. Heavyweight, created once.
SessionInterface between Java code and database. Used for CRUD operations.
TransactionManages a single unit of work (commit/rollback). Ensures data integrity.
Query / HQLExecutes 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.xml files.
  • 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 manage SessionFactory.
    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;
}
}