Notes – Hibernate Features

Hibernate is a powerful and flexible ORM (Object-Relational Mapping) tool used in Java. It simplifies database interaction by allowing developers to work with Java objects instead of SQL queries.

Below are the key features of Hibernate explained in a simple and structured way:


1. Lightweight and Open Source

  • Hibernate is free to use.
  • It does not add extra overhead to your application.
  • Easily integrates with Java applications.

2. ORM Support

  • Maps Java classes to database tables.
  • Allows you to work with objects instead of SQL queries.
  • Handles complex relationships using annotations or XML.

JavaSQL Equivalent
ClassTable
ObjectRow
FieldColumn

3. Automatic Table Creation

  • Can generate tables based on Java entity classes.
  • Saves time by reducing manual DDL (Data Definition Language) work.

Example:
Using hibernate.hbm2ddl.auto=update, Hibernate creates or updates the schema automatically.


4. HQL (Hibernate Query Language)

  • Hibernate provides its own query language.
  • Similar to SQL but works on Java objects.
  • Allows writing database-independent queries.

Example:

from Employee where salary > 50000

5. Caching Mechanism

  • Reduces database calls by storing frequently used data.
  • Two types:
    • First-level cache: Enabled by default (Session-level).
    • Second-level cache: Optional, can be configured (SessionFactory-level).

6. Database Independence

  • Hibernate can work with different databases (MySQL, Oracle, PostgreSQL, etc.).
  • Just change the dialect and connection settingsโ€”no need to rewrite the code.

7. Lazy and Eager Loading

  • Controls how associated data is fetched:
    • Lazy Loading: Data is loaded only when accessed.
    • Eager Loading: Data is fetched immediately.

8. Automatic Dirty Checking

  • Hibernate automatically detects changes in objects.
  • Updates only changed fields in the database when session.flush() is called.

9. Transaction Management

  • Supports ACID transactions.
  • Can integrate with JTA, JDBC, Spring, etc.
  • Ensures data consistency and rollback support.

10. Annotations and XML Support

  • You can define mappings using:
    • XML files (older way).
    • JPA annotations (modern and preferred).

11. Integrated with Java EE and Spring

  • Works smoothly with Spring Framework and Java EE containers.
  • Commonly used in enterprise-level applications.

12. Built-in Connection Pool

  • Hibernate provides a basic connection pool.
  • You can also integrate it with other pools like C3P0, HikariCP, etc.

Summary Table:


FeatureDescription
LightweightMinimal overhead and open source
ORM SupportMaps Java objects to DB tables
HQLObject-based query language
CachingImproves performance
Lazy/Eager LoadingControls data fetch strategy
Auto Table GenerationCreates tables from entities
DB IndependenceEasily switch databases
Dirty CheckingAuto-update changed fields
Transaction ManagementSupports commit and rollback
Annotation/XML ConfigFlexible configuration options