org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
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=