Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't Spring Boot deduce Hibernate dialect?

I can't start up my Spring Boot application

Caused by: org.hibernate.service.spi.ServiceException: 
Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment] due 
to: Unable to determine Dialect without JDBC metadata 
(please set 'jakarta.persistence.jdbc.url' for common cases or 'hibernate.dialect' when a 
custom Dialect implementation must be provided)

Here are my application properties (all of them):

spring:
  datasource:
    username: ${POSTGRES_USERNAME:postgres}
    password: ${POSTGRES_PASSWORD:postgres}
    url: ${POSTGRES_URL:jdbc:postgresql://localhost:5432/${POSTGRES_USERNAME}}

Here's my pom. I tried to trim to include only relevant parts (while ensuring it's still a valid pom you can copy and paste). Basically, to reproduce it, you only need to create a Boot project with a Data starter (maybe the driver too) so that Boot's autoconfiguration (the one with a DataSource) kicks in

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.2.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>token-service</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>token-service</name>
    <description>token-service</description>
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
        </dependency>
    </dependencies>
</project>

If I explicitly set the spring.jpa.database-platform property, it starts up fine. However, I never did it before when I used MySQL

spring:
# ...
  jpa:
    database-platform: org.hibernate.dialect.PostgreSQLDialect

Why can't Boot deduce the dialect?

like image 849
Sergey Zolotarev Avatar asked Sep 05 '25 03:09

Sergey Zolotarev


2 Answers

One reason you might see this error is if your spring.datasource definition is invalid. For example, I had the incorrect port in my url.

Spring could not connect to the database to determine the dialect and I got this message.

like image 196
IcedDante Avatar answered Sep 07 '25 21:09

IcedDante


Two possible reasons for that error:

  1. Wrong hostname/database url. Check your database connection string.
  2. Missing or wrong hibernate dialect. Add the following property spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect for MySQL database and spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect for PostgreSQL. You can check the suitable dialect for your database in the package org.hibernate.dialect of hibernate-core dependency.

Note: For MySQL if you use org.hibernate.dialect.MySQL8Dialect it will tell you that it's deprecated thus use org.hibernate.dialect.MySQLDialect

like image 32
Vladimir Avatar answered Sep 07 '25 20:09

Vladimir