✅ What is a Configuration object in Hibernate?
Answer:
A Configuration object in Hibernate is a class that loads and holds all the settings, mappings, and properties required to bootstrap Hibernate and build a SessionFactory
.
🔹 Key responsibilities of Configuration:
- Reads configuration files like
hibernate.cfg.xml
orhibernate.properties
. - Loads mapping metadata (e.g., XML
.hbm.xml
files or annotated entity classes). - Stores database connection details, dialect, caching, and other Hibernate-specific settings.
- Acts as the starting point for creating the
SessionFactory
.
🔹 Typical usage:
When configuring Hibernate manually (e.g., in a standalone Java SE app), you build the SessionFactory
like this:
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml"); // reads Hibernate configuration file
SessionFactory sessionFactory = config.buildSessionFactory();
🔹 What happens internally:
Configuration.configure()
parses the config file(s) and sets up properties.addAnnotatedClass(...)
or mapping files can be registered programmatically if needed.- When you call
buildSessionFactory()
, it processes all mappings and creates a fully initializedSessionFactory
.
🔹 Example adding annotated classes manually:
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml");
config.addAnnotatedClass(User.class);
SessionFactory sessionFactory = config.buildSessionFactory();
🔹 In Spring Boot:
- You almost never use
Configuration
directly. - Spring Boot uses
LocalSessionFactoryBuilder
and auto-configuration to build theSessionFactory
behind the scenes based on yourapplication.properties
.
✅ Key takeaway:
The Hibernate Configuration object is the central place for loading settings and entity mappings, and is the first step in setting up Hibernate manually — it prepares everything needed to build the SessionFactory
.
🔹 1) Manual Configuration (Standalone Hibernate in Java SE)
hibernate.cfg.xml file:
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/mydb</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- You can also specify mapping files or use annotated classes -->
<!-- <mapping resource="com/example/User.hbm.xml"/> -->
</session-factory>
</hibernate-configuration>
Java code to build SessionFactory manually:
Configuration config = new Configuration();
config.configure("hibernate.cfg.xml"); // loads the above XML
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
// Use session here...
tx.commit();
session.close();
sessionFactory.close();
✅ Manual approach is best for standalone apps, but requires writing and maintaining hibernate.cfg.xml
.
🔹 2) Spring Boot Configuration via application.properties
In a Spring Boot project with spring-boot-starter-data-jpa
and Hibernate on the classpath, no manual SessionFactory
creation needed — you just configure properties:
application.properties:
# Database connection
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# Hibernate-specific settings
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
# Optional HikariCP tuning (used by Spring Boot by default)
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.minimum-idle=2
Usage in Spring Boot service:
@Service
public class UserService {
@Autowired
private EntityManager entityManager;
public User findUser(Long id) {
return entityManager.find(User.class, id);
}
}
✅ Spring Boot auto-configures Hibernate by reading these properties and builds a SessionFactory
behind the scenes — you don’t need to create it manually.
🔹 Key takeaway:
- Manual Hibernate setup →
hibernate.cfg.xml
+Configuration
class to buildSessionFactory
. - Spring Boot → simple
application.properties
→ Spring Boot auto-wires everything for you.