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.
When explicit configuration helps: specify a dialect when JDBC metadata is unavailable, startup must avoid database access, or the application uses a custom or community dialect.

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 familyTypical Hibernate dialectNotes
H2org.hibernate.dialect.H2DialectUseful for local development and tests.
MySQLorg.hibernate.dialect.MySQLDialectUse a current MySQL version supported by your Hibernate release.
MariaDBorg.hibernate.dialect.MariaDBDialectUse the MariaDB dialect when the server is MariaDB.
PostgreSQLorg.hibernate.dialect.PostgreSQLDialectSupports PostgreSQL-specific SQL and types.
Oracleorg.hibernate.dialect.OracleDialectCheck the minimum Oracle version for your Hibernate release.
Microsoft SQL Serverorg.hibernate.dialect.SQLServerDialectSupports SQL Server syntax and capabilities.
DB2org.hibernate.dialect.DB2DialectUse the matching DB2 family dialect where required.
HSQLDBorg.hibernate.dialect.HSQLDialectCommon in lightweight test environments.
SAP HANAorg.hibernate.dialect.HANADialectUse a compatible Hibernate and HANA combination.
Sybaseorg.hibernate.dialect.SybaseDialectSome 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

  1. Use a Hibernate version compatible with the target database version.
  2. Prefer automatic dialect resolution in Hibernate 6+ when JDBC metadata is available.
  3. When configuring a dialect explicitly, use the class documented for your Hibernate release.
  4. Do not copy version-specific dialect names from old tutorials without checking the current API.
  5. Test generated SQL, pagination, schema validation, sequences, and database-specific types.
  6. Use Flyway, Liquibase, or another migration tool for production schema changes.
Summary: a dialect is Hibernate's database-specific SQL translator. In modern Hibernate, automatic resolution is usually enough; explicit configuration is reserved for special deployment or compatibility requirements.