Reputation: 9065
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?
Upvotes: 384
Views: 785766
Reputation: 645
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
file:
# Replace with your connection string
spring.datasource.url=jdbc:mysql://localhost:3306/pdb1
# Replace with your credentials
spring.datasource.username=root
spring.datasource.password=
Upvotes: 23
Reputation: 7171
In my case, mssql
use
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect" />
in persistence.xml
.
Upvotes: 0
Reputation: 411
Because there is a lot of configuration to set this up, make sure you have not had a typo!
Upvotes: 0
Reputation: 1129
I have experienced similar issue, but my application would start and it happened rapidly. I discovered when this happened. Mainly, when I started to 'mongodump' mongo database. It is weird, because it's unrelated to PostgreSQL. When I killed mongodump command, my ap started to build again. This app make use of two databases, one PostgreSql and second is mongoDB. Not obvious.
Upvotes: 0
Reputation: 562
I solved this problem by adding in hibernate.cfg.xml
:
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
Granted, I upgraded Spring and Hibernate on a very old application and did not refactor it to new standards, thus for most people, this solution is irrelevant as it applies only to application configured the old fashion way.
Upvotes: -1
Reputation: 6629
Checking the code (HibernateJpaVendorAdapter && JdbcEnvironmentInitiator), the fallback looks like this:
Upvotes: 0
Reputation: 6450
As a more descriptive answer
To fix the issue which is related to connecting to the database:
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:100) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:54) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:137) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator.initiateService(JdbcEnvironmentInitiator.java:35) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:101) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ~[hibernate-core-5.4.8.Final.jar:5.4.8.Final]
... 38 common frames omitted
everything you need to do is:
Data Source
configuration option related to the required database (which you are using inside your application.properties
file), e.g. you have spring.datasource.platform=postgres
:"FATAL: database "testDb" does not exist"
while connecting:using PgAdmin
needs to create manually the database (if you are using the development platform for PostgreSQL
):
based on your property inside application.properties
file:
spring.datasource.url=jdbc:postgresql://localhost:5432/testDb
based on your application.properties
file:
"Test Connection"
and "Apply"
.Upvotes: 0
Reputation: 2484
For me, The problem solved by doing as following :
Right click on Entities on Persistence window and select related data source
Upvotes: 0
Reputation: 153780
If you're using Spring Boot, you don't need to provide the JPA and Hibernate configuration explicitly, as Spring Boot can do that for you.
In the application.properties
Spring Boot configuration file, you have the add your database configuration properties:
spring.datasource.driverClassName = org.postgresql.Driver
spring.datasource.url = jdbc:postgresql://localhost:5432/teste
spring.datasource.username = klebermo
spring.datasource.password = 123
And, in the same application.properties
configuration file, you can also set custom Hibernate properties:
# Log SQL statements
spring.jpa.show-sql = false
# Hibernate ddl auto for generating the database schema
spring.jpa.hibernate.ddl-auto = create
# Hibernate database Dialect
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
That's it!
Upvotes: 17
Reputation: 1638
It turns out there is no one mentioning set spring.jpa.database=mysql
in application.properties
file, if you use Spring JPA. This is the simplest answer to me and I want to share in this question.
Upvotes: 4
Reputation: 8280
In my case, the root cause of this exception comes from using an old version mysql-connector and I had this error :
unable to load authentication plugin 'caching_sha2_password'. mysql
Adding this line to the mysql server configuration file (my.cnf or my.ini) fix this issue :
default_authentication_plugin=mysql_native_password
Upvotes: 0
Reputation: 124526
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.
Upvotes: 312
Reputation: 138
I reproduced this error message in the following three cases:
The obvious solution is to create new database user with the same username and password as in the spring-boot app or change username and password in your spring-boot app files to match an existing database user and grant sufficient privileges to that database user. In case of MySQL database this can be done as shown below:
mysql -u root -p
>CREATE USER 'theuser'@'localhost' IDENTIFIED BY 'thepassword';
>GRANT ALL ON *.* to theuser@localhost IDENTIFIED BY 'thepassword';
>FLUSH PRIVILEGES;
Obviously there are similar commands in Postgresql but I haven't tested if in case of Postgresql this error message can be reproduced in these three cases.
Upvotes: 2
Reputation: 1110
I had same issue. adding this to the application.properties solved the issue:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
Upvotes: 15
Reputation: 514
Adding spring.jpa.database-platform=org.hibernate.dialect.MariaDB53Dialect
to my properties file worked for me.
PS: i'm using MariaDB
Upvotes: 1
Reputation: 1582
The following are some of the reasons for the hibernate.dialect
not set issue.
Most of these exceptions are shown in the startup log which is finally followed by the mentioned issue.
Example: In Spring boot app with Postgres DB
org.postgresql.util.PSQLException: FATAL: database "foo" does not exist
In application.properties
file,
spring.datasource.url = jdbc:postgresql://localhost:5432/foo
but foo
didn't exist.
So I created the database from pgAdmin for postgres
CREATE DATABASE foo;
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
spring.datasource.username= {DB USERNAME HERE}
spring.datasource.password= {DB PASSWORD HERE}
Upvotes: 9
Reputation: 7665
I had the same Error,
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
in my case the WAR had application.properties inside that pointed to development server
where external application.properties was pointing to the right DB server.
make sure you don't have any other application.properties in the classpath / jars...
Upvotes: 0
Reputation: 7665
I had the same issue and after debugging it turns out that Spring application.properties had wrong IP address for DB server
spring.datasource.url=jdbc:oracle:thin:@WRONG:1521/DEV
Upvotes: 2
Reputation: 1482
If the preceding error in log was this: "ERROR - HikariPool-1 - jdbcUrl is required with driverClassName" then the solution is to rewrite "url" to "jdbc-url" according to this: Database application.yml for Spring boot from applications.properties
Upvotes: 0
Reputation: 1966
I also had this problem. In my case it was because of no grants were assigned to MySQL user. Assigning grants to MySQL user which my app uses resolved the issue:
grant select, insert, delete, update on my_db.* to 'my_user'@'%';
Upvotes: 1
Reputation: 111
In spring boot for jpa java config you need to extend JpaBaseConfiguration and implement it's abstract methods.
@Configuration
public class JpaConfig extends JpaBaseConfiguration {
@Override
protected AbstractJpaVendorAdapter createJpaVendorAdapter() {
final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
return vendorAdapter;
}
@Override
protected Map<String, Object> getVendorProperties() {
Map<String, Object> properties = new HashMap<>();
properties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
}
}
Upvotes: 6
Reputation: 4180
I got this issue when Eclipse was unable to find the JDBC driver. Had to do a gradle refresh from the eclipse to get this work.
Upvotes: 2
Reputation: 622
I had the same error after using the hibernate code generation
https://www.mkyong.com/hibernate/how-to-generate-code-with-hibernate-tools/
then the hibernate.cfg.xml
was created in /src/main/java
but without the connection parameters
after removing it - my problem was solved
Upvotes: 0
Reputation: 1
I faced this issue due to Mysql 8.0.11 version reverting back to 5.7 solved for me
Upvotes: 0
Reputation: 1056
For those working with AWS MySQL RDS, it may occur when you are unable to connect to the database. Go to AWS Security Groups setting for MySQL RDS and edit the inbound IP rule by refreshing MyIP.
I faced this issue and doing above got the problem fixed for me.
Upvotes: 1
Reputation: 2768
Same but in a JBoss WildFly AS.
Solved with properties in my META-INF/persistence.xml
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="spring.jpa.database-platform" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="spring.jpa.show-sql" value="false" />
</properties>
Upvotes: 1
Reputation: 11
I faced the same issue: The db I was trying to connect did not exist. I used jpa.database=default
(which I guess means it will try to connect to the database and then auto select the dialect). Once I started the database, it worked fine without any change.
Upvotes: 0
Reputation: 883
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.
Upvotes: 0
Reputation: 1
This happened to me because I hadn't added the conf.configure();
before beginning the session:
Configuration conf = new Configuration();
conf.configure();
Upvotes: 0
Reputation: 3777
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.
Upvotes: 1