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

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

Kleber Mota
1#
Kleber Mota Published in 2014-10-24 13:06:54Z

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?

M. Deinum
2#
M. Deinum Reply to 2014-10-24 13:56:25Z

First remove all of your configuration Spring Boot will start it for you. If you really need a SessionFactory instead of an EntityManagerFactory add a HibernateJpaSessionFactoryBean.

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

spring.datasource.driverClassName=org.postgresql.Driver
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

Now if you really need access to a SessionFactory and that is basically for the same datasource you can do the following (Which is also docmented 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.

Assuming you have an 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).

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

mhnagaoka
3#
mhnagaoka Reply to 2015-04-08 16:23:46Z

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.

ACV
4#
ACV Reply to 2015-09-05 19:14:51Z

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

Eric B.
5#
Eric B. Reply to 2015-10-09 03:43:59Z

I ran into the same problem and my issue was that the DB I was trying to connect to didn't exist.

I created the DB, verified the URL/connection string and reran and everything worked as expected.

Pankaj Soni
6#
Pankaj Soni Reply to 2015-11-08 18:56:43Z

I also faced 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. It 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=
Michał
7#
Michał Reply to 2017-01-05 21:22:54Z

Make sure your application.properties has all correct info: (I changed my db port from 8889 to 3306 it worked)

 db.url: jdbc:mysql://localhost:3306/test
zbig
8#
zbig Reply to 2016-10-13 09:48:15Z

In my case the user could not connect to the database. If will have same issue if the log contains a warning just before the exception:

WARN HHH000342: Could not obtain connection to query metadata : Login failed for user 'my_user'.
user7169604
9#
user7169604 Reply to 2016-11-16 20:15:58Z

this is happening because your code is not bale to connect the database. Make sure you have mysql driver and username, password correct.

obesechicken13
10#
obesechicken13 Reply to 2017-02-17 21:10:16Z

Make sure you have your database in your pom like OP did. That was my problem.

doesnt_matter
11#
doesnt_matter Reply to 2017-03-25 03:11:02Z

My problem was that embedded database was already connected. close connection

Farouk
12#
Farouk Reply to 2017-05-03 19:25:43Z

I had the same issue and it was caused by being unable to connect to the database instance. Look for hibernate error HHH000342 in the log above that error, it should give you an idea to where the db connection is failing (incorrect username/pass, url, etc.)

alpine
13#
alpine Reply to 2017-05-27 10:01:43Z

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

zygimantus
14#
zygimantus Reply to 2017-05-28 20:20:26Z

If you are using this line:

sessionFactory.getHibernateProperties().put("hibernate.dialect", env.getProperty("hibernate.dialect"));

make sure that env.getProperty("hibernate.dialect") is not null.

Michael
15#
Michael Reply to 2017-12-05 17:06:49Z

This happened to me because I hadn't added the conf.configure(); before beginning the session:

Configuration conf = new Configuration();
conf.configure();
user1648825
16#
user1648825 Reply to 2017-12-05 17:22:01Z

After swearing for a while, I restarted my IDE and it starts to work :). So sometimes a solution could be very easy.

Lahiru Gamage
17#
Lahiru Gamage Reply to 2018-01-09 15:10:35Z

Make sure that you have enter valid detail in application.properties and whether your database server is available. As a example when you are connecting with MySQL check whether XAMPP is running properly.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.37074 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO