The Hibernate configuration file, usually hibernate.cfg.xml, tells Hibernate how to connect to a database, which entity classes to map and which behaviour settings to apply when it builds a SessionFactory. It is one of several ways to configure Hibernate: the same settings can also live in persistence.xml, a hibernate.properties file, Java code, or Spring Boot properties.
This article shows what belongs in the file, a working Hibernate 6/7 example, and when to prefer a different configuration style.
When you bootstrap Hibernate natively (without JPA's persistence.xml and without Spring), the file is the central place where you describe the environment. Hibernate reads it from the classpath root when you call configure() on a StandardServiceRegistryBuilder. The file has three jobs:
A file named hibernate.cfg.xml is only a convention. You can pass any resource name to configure("my-config.xml").
Properties starting with jakarta.persistence. are the standard JPA names used by Hibernate 6 and later. Older tutorials use hibernate.connection.url, hibernate.connection.username and so on, and Hibernate still accepts those. Before Hibernate 6, the standard names started with javax.persistence..
Hibernate needs a JDBC URL, user and password, or a DataSource supplied by an application server. Do not commit real passwords to the file. Read them from environment variables or a secrets store and set them in code before building the factory.
The built-in connection pool is meant for tests only. For production, add a real pool, typically HikariCP through the hibernate-hikaricp module, and set hibernate.hikari.maximumPoolSize and related properties.
Since Hibernate 6 the dialect is detected from the database metadata in most cases, so hibernate.dialect is no longer required. Set it only when auto-detection picks the wrong one, or when you need a specific database version behaviour.
hibernate.hbm2ddl.auto controls what Hibernate does with the schema.
| Value | Effect |
|---|---|
none | Do nothing |
validate | Check that the schema matches the mappings and fail on startup if not |
update | Add missing tables and columns |
create | Drop and recreate the schema at startup |
create-drop | Create at startup, drop when the factory closes |
Use validate or none in production and manage the schema with a migration tool such as Flyway or Liquibase. update and create are for local development and tests.
Register annotated classes with <mapping class="..."/>, or older XML mappings with <mapping resource="User.hbm.xml"/>. You can also register classes in code with MetadataSources.addAnnotatedClass, which is useful when the list is built dynamically.
Second-level and query caching are off by default. To enable them, add a cache provider module such as hibernate-jcache, then set hibernate.cache.use_second_level_cache and, if needed, hibernate.cache.use_query_cache to true. Mark entities with @Cacheable or the Hibernate cache annotations. Other common settings are hibernate.jdbc.batch_size, hibernate.default_schema and hibernate.highlight_sql.
Using it:
Create one SessionFactory per database for the whole application. It is expensive to build and thread safe, while a Session is cheap and must not be shared between threads.
| Approach | When it fits |
|---|---|
persistence.xml | You use the JPA API (EntityManagerFactory) and want portable configuration |
hibernate.properties | Only simple key-value settings, no mappings list |
Programmatic Configuration or StandardServiceRegistryBuilder.applySetting | Settings come from environment or a config service |
Spring Boot application.properties | Spring Boot application; use spring.datasource.* and spring.jpa.properties.hibernate.* |
In a Spring Boot application you normally do not use hibernate.cfg.xml at all. Boot builds the EntityManagerFactory from its own properties and scans entities automatically.
src/main/resources.hbm2ddl.auto as update in production, which can alter schemas unexpectedly.Unknown entity errors.javax.persistence property names with Hibernate 6 or later.No. It is required only for native bootstrapping when you call configure() without arguments. JPA, Spring Boot and programmatic setups do not need it.
hibernate.cfg.xml is Hibernate-specific and used with the native SessionFactory API. persistence.xml is the JPA standard and lives in META-INF, used with EntityManagerFactory.
Yes. Call configure("other.cfg.xml") to build a separate SessionFactory, for example for a second database.
Most new projects use Spring Boot or Jakarta Persistence with persistence.xml. Use hibernate.cfg.xml only when you bootstrap Hibernate natively and want settings outside the code.
The Hibernate configuration file defines the database connection, Hibernate behaviour and the mapped entities used to build a SessionFactory. Use the jakarta.persistence.* property names with modern Hibernate, validate the schema instead of auto-updating in production, use a real connection pool, keep secrets out of the file, and prefer Spring Boot properties or persistence.xml when your framework already provides them.