The Dialect class in Hibernate describes a specific database to the ORM. It tells Hibernate which SQL syntax to generate for pagination, identity and sequence columns, data types, built-in functions and locking, so the same HQL or JPQL query works on PostgreSQL, MySQL, Oracle, SQL Server or H2. Hibernate 6 and later detect the dialect automatically from the JDBC connection, so you rarely need to set it.
Set it explicitly only when detection is not possible, for example when the application starts without a database connection, or when you need a custom subclass.
A Dialect (package org.hibernate.dialect) is the single place where database differences are encoded. It decides, among other things:
LIMIT and OFFSET pagination is written (LIMIT, FETCH FIRST, TOP and so on).uuid, jsonb or number.concat, coalesce or current_timestamp translate to native SQL.for update skip locked.Because of this, switching database engines is largely a matter of changing the JDBC URL and driver. The dialect adapts the generated SQL.
Since Hibernate 6, the dialect is resolved from the database metadata (product name, major and minor version) when Hibernate connects. Spring Boot does not need spring.jpa.properties.hibernate.dialect for common databases.
The Hibernate 6 documentation recommends not setting the property unless required. In the 6.x line the version-specific classes such as MySQL8Dialect, PostgreSQL95Dialect and Oracle12cDialect were deprecated in favour of the plain classes, which adapt to the detected database version.
| Database | Dialect class (Hibernate 6) |
|---|---|
| PostgreSQL | org.hibernate.dialect.PostgreSQLDialect |
| MySQL | org.hibernate.dialect.MySQLDialect |
| MariaDB | org.hibernate.dialect.MariaDBDialect |
| Oracle | org.hibernate.dialect.OracleDialect |
| SQL Server | org.hibernate.dialect.SQLServerDialect |
| H2 | org.hibernate.dialect.H2Dialect |
Older articles name Oracle12cDialect or MySQL8Dialect. If you upgrade from Hibernate 5, replace those with the classes above, or remove the setting entirely and let Hibernate detect it. Newer Hibernate versions may reorganise the community dialects, so check the release notes of the version you use.
In a Spring Boot application, use a property:
In plain Hibernate, use hibernate.cfg.xml or persistence.xml:
When the property is present, Hibernate uses it and skips detection. A wrong dialect is a common cause of confusing errors such as invalid LIMIT syntax or unknown column types, so keep it in line with the real database.
To see how the dialect shapes the SQL, enable logging in a test or development profile:
A paginated query for the same repository method produces different SQL per dialect:
The exact text depends on the Hibernate and database versions. The point is that you write one query and the dialect produces the right statement.
You can extend an existing dialect to register a database function that Hibernate does not know. The cleaner approach in Hibernate 6 is a FunctionContributor, registered through the Java service loader file META-INF/services/org.hibernate.boot.model.FunctionContributor.
After registration, you can call levenshtein(name, :term) in HQL on a database that provides that function, such as PostgreSQL with the fuzzystrmatch extension. Subclassing a dialect directly is still possible for deeper changes, but keep it small, because dialect internals change between major versions.
Usually not. Hibernate 6 and later detect it from the connection. Set it only when detection fails or you use a custom dialect.
Hibernate may generate invalid SQL, such as unsupported pagination or type names, and fail at runtime or during schema generation.
Newer Hibernate versions use a single MySQLDialect that adapts to the connected server version, so version-specific classes are no longer needed.
No. Native queries are sent as written. The dialect affects generated SQL from HQL, JPQL, the Criteria API and schema tools.
The Hibernate Dialect is the adapter between portable queries and database-specific SQL. Let Hibernate detect it, use the unversioned classes if you must set it, check generated SQL when behaviour looks odd, and use a FunctionContributor for small customisations.