Hibernate Database Configuration
SQL Dialects in Hibernate
Understand how Hibernate adapts SQL, data types, pagination, and schema generation to different relational databases.
What Is a Hibernate Dialect?
A dialect is a Hibernate service that describes database-specific SQL capabilities and translates Hibernate operations into SQL supported by that database. It helps Hibernate handle differences in functions, data types, pagination, sequences, identity columns, and DDL.
Dialect classes are located in the org.hibernate.dialect package. The correct choice depends on the Hibernate ORM version and the database version.
Automatic Dialect Resolution
Hibernate 6 and later can usually determine the dialect by reading JDBC metadata. In a normal application, you often do not need to set hibernate.dialect manually.
hibernate.connection.url=jdbc:postgresql://localhost:5432/company hibernate.connection.username=app_user hibernate.connection.password=change-me # Hibernate resolves PostgreSQL from JDBC metadata.
Explicit Dialect Configuration
Use the fully qualified dialect class name in a properties file or Hibernate configuration file when you need to select it explicitly.
<property name="hibernate.dialect"> org.hibernate.dialect.PostgreSQLDialect </property>
Older tutorials may show version-specific names such as MySQL8Dialect or Oracle12cDialect. Current Hibernate versions generally use the unversioned dialect class and infer supported behavior from the database. Always check the documentation for the exact Hibernate version used by the project.
Common Built-in Dialects
| Database family | Typical Hibernate dialect | Notes |
|---|---|---|
| H2 | org.hibernate.dialect.H2Dialect | Useful for local development and tests. |
| MySQL | org.hibernate.dialect.MySQLDialect | Use a current MySQL version supported by your Hibernate release. |
| MariaDB | org.hibernate.dialect.MariaDBDialect | Use the MariaDB dialect when the server is MariaDB. |
| PostgreSQL | org.hibernate.dialect.PostgreSQLDialect | Supports PostgreSQL-specific SQL and types. |
| Oracle | org.hibernate.dialect.OracleDialect | Check the minimum Oracle version for your Hibernate release. |
| Microsoft SQL Server | org.hibernate.dialect.SQLServerDialect | Supports SQL Server syntax and capabilities. |
| DB2 | org.hibernate.dialect.DB2Dialect | Use the matching DB2 family dialect where required. |
| HSQLDB | org.hibernate.dialect.HSQLDialect | Common in lightweight test environments. |
| SAP HANA | org.hibernate.dialect.HANADialect | Use a compatible Hibernate and HANA combination. |
| Sybase | org.hibernate.dialect.SybaseDialect | Some Sybase variants use separate dialect classes. |
The exact supported database versions and dialect classes change between Hibernate releases. Treat this table as a starting point, not a substitute for the version-specific Hibernate dialect documentation.
Legacy and Community Dialects
Older Hibernate references list classes such as Oracle9Dialect, Oracle10gDialect, MySQLInnoDBDialect, MySQLMyISAMDialect, SAPDBDialect, InformixDialect, IngresDialect, FirebirdDialect, and InterbaseDialect. Some are obsolete, renamed, moved, or no longer maintained in hibernate-core.
Since Hibernate 6, some less common dialects are distributed in the optional hibernate-community-dialects artifact. Community dialects may have different compatibility and support expectations from dialects maintained in core.
<dependency> <groupId>org.hibernate.orm</groupId> <artifactId>hibernate-community-dialects</artifactId> <version>6.6.9.Final</version> </dependency>
Only add this dependency when the selected dialect actually requires it, and keep its version aligned with hibernate-core.
What a Dialect Does Not Do
- It does not create a database connection; the JDBC driver and connection settings do that.
- It does not replace the database JDBC driver.
- It does not make every database feature portable.
- It does not remove the need for schema migrations in production.
Best Practices
- Use a Hibernate version compatible with the target database version.
- Prefer automatic dialect resolution in Hibernate 6+ when JDBC metadata is available.
- When configuring a dialect explicitly, use the class documented for your Hibernate release.
- Do not copy version-specific dialect names from old tutorials without checking the current API.
- Test generated SQL, pagination, schema validation, sequences, and database-specific types.
- Use Flyway, Liquibase, or another migration tool for production schema changes.
Reference: Hibernate ORM supported dialects
