org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set

553,636

Solution 1

First remove all of your configuration Spring Boot will start it for you.

Make sure you have an application.properties in your classpath and add the following properties.

spring.datasource.url=jdbc:postgresql://localhost:5432/teste?charSet=LATIN1
spring.datasource.username=klebermo
spring.datasource.password=123

spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.show-sql=false
spring.jpa.hibernate.ddl-auto=create

If you really need access to a SessionFactory and that is basically for the same datasource, then you can do the following (which is also documented here although for XML, not JavaConfig).

@Configuration        
public class HibernateConfig {

    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory(EntityManagerFactory emf) {
         HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
         factory.setEntityManagerFactory(emf);
         return factory;
    }
}

That way you have both an EntityManagerFactory and a SessionFactory.

UPDATE: As of Hibernate 5 the SessionFactory actually extends the EntityManagerFactory. So to obtain a SessionFactory you can simply cast the EntityManagerFactory to it or use the unwrap method to get one.

public class SomeHibernateRepository {

  @PersistenceUnit
  private EntityManagerFactory emf;

  protected SessionFactory getSessionFactory() {
    return emf.unwrap(SessionFactory.class);
  }

}

Assuming you have a class with a main method with @EnableAutoConfiguration you don't need the @EnableTransactionManagement annotation, as that will be enabled by Spring Boot for you. A basic application class in the com.spring.app package should be enough.

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {


    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

} 

Something like that should be enough to have all your classes (including entities and Spring Data based repositories) detected.

UPDATE: These annotations can be replaced with a single @SpringBootApplication in more recent versions of Spring Boot.

@SpringBootApplication
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
} 

I would also suggest removing the commons-dbcp dependency as that would allow Spring Boot to configure the faster and more robust HikariCP implementation.

Solution 2

I was facing a similar problem when starting up the application (using Spring Boot) with the database server down.

Hibernate can determine the correct dialect to use automatically, but in order to do this, it needs a live connection to the database.

Solution 3

add spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect in application.properties file

Solution 4

I got this error when my database was not created. After creating the DB manually, it worked fine.

Solution 5

I also faced a similar issue. But, it was due to the invalid password provided. Also, I would like to say your code seems to be old-style code using spring. You already mentioned that you are using spring boot, which means most of the things will be auto configured for you. hibernate dialect will be auto selected based on the DB driver available on the classpath along with valid credentials which can be used to test the connection properly. If there is any issue with the connection you will again face the same error. only 3 properties needed in application.properties

# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3306/pdb1

# Replace with your credentials
spring.datasource.username=root
spring.datasource.password=
Share:
553,636

Related videos on Youtube

Kleber Mota
Author by

Kleber Mota

Updated on July 08, 2022

Comments

  • Kleber Mota
    Kleber Mota almost 2 years

    I am trying run a spring-boot application which uses hibernate via spring-jpa, but i am getting this error:

    Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
            at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104)
            at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:71)
            at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:205)
            at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
            at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
            at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1885)
            at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1843)
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843)
            at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
            at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842)
            at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
            at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:336)
            at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1613)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1550)
            ... 21 more
    

    my pom.xml file is this:

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-web</artifactId>
           </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-taglibs</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
        </dependency>
    </dependencies>
    

    my hibernate configuration is that (the dialect configuration is in the last method from this class):

    @Configuration
    @EnableTransactionManagement
    @ComponentScan({ "com.spring.app" })
    public class HibernateConfig {
    
       @Bean
       public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    
          sessionFactory.setDataSource(restDataSource());
          sessionFactory.setPackagesToScan(new String[] { "com.spring.app.model" });
          sessionFactory.setHibernateProperties(hibernateProperties());
    
          return sessionFactory;
       }
    
       @Bean
       public DataSource restDataSource() {
          BasicDataSource dataSource = new BasicDataSource();
    
          dataSource.setDriverClassName("org.postgresql.Driver");
          dataSource.setUrl("jdbc:postgresql://localhost:5432/teste?charSet=LATIN1");
          dataSource.setUsername("klebermo");
          dataSource.setPassword("123");
    
          return dataSource;
       }
    
       @Bean
       @Autowired
       public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
          HibernateTransactionManager txManager = new HibernateTransactionManager();
          txManager.setSessionFactory(sessionFactory);
          return txManager;
       }
    
       @Bean
       public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
       }
    
       Properties hibernateProperties() {
          return new Properties() {
             /**
             * 
             */
            private static final long serialVersionUID = 1L;
    
            {
                setProperty("hibernate.hbm2ddl.auto", "create");
                setProperty("hibernate.show_sql", "false");
                setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
             }
          };
       }
    }
    

    what I am doing wrong here?

  • Kleber Mota
    Kleber Mota over 9 years
    but is there any way to avoid create this file application.propertes? I prefer a way where I do all the configuration in the class HibernateConfig.
  • M. Deinum
    M. Deinum over 9 years
    Why? The whole purpose of Spring Boot is that it does auto configuration for you... So you prefer to include a verbose java configuration over 7 lines in a properties file?!
  • Andy Wilkinson
    Andy Wilkinson over 9 years
    It could even be 6 lines in a property file. You don't need to set spring.datasource.driverClassName when you're using Postgres as Boot will infer it from spring.datasource.url.
  • Brian
    Brian almost 9 years
    My problem was similar to this in that pg_hba.conf on the server wasn't configured for the remote connection, which gave me this error.
  • Jonas Pedersen
    Jonas Pedersen over 8 years
    If the database you are trying to connect to does not exists you also see this error.
  • Alex Worden
    Alex Worden over 8 years
    application.properties files are a bad way to configure software. You cannot presume there will be a file-system (and you wouldn't want to use it). Bundling it in the .jar or .war means you have deployment specific builds which is a very bad idea. Also, it's insecure since your passwords are on disk.
  • M. Deinum
    M. Deinum over 8 years
    @AlexWorden No it isn't... Instead of putting random comments you might first want to read how properties are loaded especially the support Spring Boot has. Passwords on disk don't have to be a problem if you set the file rights straight. Putting them in a database isn't much better...
  • AdrianVeidt
    AdrianVeidt over 8 years
    I have multiple datasources, which is why i can't autoconfigure (I think). Still looking for solution to this.
  • Feras
    Feras over 8 years
    Thanks you comment led me to check connection port which was wrong
  • Pankaj
    Pankaj over 8 years
    Instead of using 3 annotations i.e. Configuration,EnableAutoConfiguration,ComponentScan. We can use SpringBootApplication annotation
  • Andrei
    Andrei over 8 years
    Normally you can avoid manual creation by using "spring.jpa.hibernate.ddl-auto=create"
  • Federico Piazza
    Federico Piazza over 7 years
    Got the same problem, in my case the DB was up but the credentials were wrong. +1
  • Alex Worden
    Alex Worden over 7 years
    @Andrei - how / where would I specify "spring.jpa.hibernate.ddl-auto=create"?
  • ACV
    ACV over 7 years
    @AlexWorden, Andrei meant you can put it in application.properties if it is Spring Boot. There must be the equivalent for xml based Spring configuration.
  • Tunaki
    Tunaki about 7 years
    Can you elaborate further? What do you mean with this answer?
  • obesechicken13
    obesechicken13 about 7 years
    I just needed to add the mysql dependency.
  • Saurabh
    Saurabh about 7 years
    surprisingly i started getting this issue when dropped schema expecting hbm2ddl.auto=true will create db and tables. however it failed. but wen i created empty schema hbm2ddl worked creating tables
  • borjab
    borjab about 7 years
    I can confirm that there is some kind of Hibernate problem that reports this error when it fails to connect. I could reproduce the problem switching on and off the network.
  • zygimantus
    zygimantus over 6 years
    Does this error appear only when the database does not exist or also when the tables inside the database are not present?
  • ACV
    ACV over 6 years
    Only when there is no DB
  • santu
    santu over 6 years
    I had given wrong password and I was facing the same error. Thank you ACV, you helped me a lot.
  • Illidan
    Illidan about 5 years
    IP of database machine has been changed but a DNS was resolving to the old one :-(
  • Taylor
    Taylor about 5 years
    This is a great answer - allows me to use a MockBean for my database class and not have real connection information in my properties profile
  • chrips
    chrips almost 5 years
    Oh man... you saved me. I had changed my password... and that connection issue was enough to create all the errors
  • sem10
    sem10 over 4 years
    I allowed all traffic to the security group of the RDS Aurora MySQL, this did the trick for me. If u want to be more specific or for prod env, add only the allowed ips
  • fatih yavuz
    fatih yavuz about 4 years
    some cases application.properties cannot see the properties changes.And i added properties.put like above and worked fine.Thx for reply.
  • Maks
    Maks almost 4 years
    Adding spring.jpa.properties.hibernate.dialect to the application.properties helped me. Thank you)
  • Maks
    Maks almost 4 years
    Adding spring.jpa.properties.hibernate.dialect to the application.properties helped me. Thank you)
  • Maks
    Maks almost 4 years
    Adding spring.jpa.properties.hibernate.dialect to the application.properties helped me. Thank you)
  • AlikElzin-kilaka
    AlikElzin-kilaka almost 4 years
    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQ‌​LDialect was enough for me.
  • 袁文涛
    袁文涛 over 3 years
    org.hibernate.dialect.PostgreSQLDialect is deprecated, use org.hibernate.dialect.PostgreSQL82Dialect instead
  • David Siegal
    David Siegal over 3 years
    Hibernate continues to release updated dialects. See: docs.jboss.org/hibernate/orm/current/javadocs/org/hibernate/‌​…
  • Julisch
    Julisch over 3 years
    Was the same for me!
  • Dhruvam Gupta
    Dhruvam Gupta over 3 years
    For me as well, DB server was not up and that's why this error came up while deploying the spring boot app.
  • ADJenks
    ADJenks over 2 years
    Why doesn't it throw a more obvious error first, like "Cannot connect to database" or something?
  • Amulya Koppula
    Amulya Koppula over 2 years
    For Oracle database, spring.jpa.database-platform=org.hibernate.dialect.OracleDia‌​lect
  • pixel
    pixel almost 2 years
    This does not work if you use multiple data sources for databases by different vendors. for example if you have one data source for DB2 and another for Oracle, this will not work.
  • pixel
    pixel almost 2 years
    This does not work if you use multiple data sources for databases by different vendors. for example if you have one data source for DB2 and another for Oracle, this will not work.
  • pixel
    pixel almost 2 years
    This does not work if you use multiple data sources for databases by different vendors. for example if you have one data source for DB2 and another for Oracle, this will not work.
  • pixel
    pixel almost 2 years
    This does not work if you use multiple data sources for databases by different vendors. for example if you have one data source for DB2 and another for Oracle, this will not work. How would you set the dialect in that case?
  • Rstar37
    Rstar37 almost 2 years
    The problem with me was just the SQL syntax, it became clear after using a postrgresql editor
  • Iceberg
    Iceberg almost 2 years
    Well, once I corrected the port number in configuration, this error disappeared.