Related
I work on a java spring boot project.
I have an application.yml file that contains the connection string:
spring:
data:
mongodb:
uri: mongodb://localhost:27017/development
I comment on connection string rows in the application.yml file:
#spring:
# data:
# mongodb:
# uri: mongodb://localhost:27017/development
And then I rebuild the project and trigger repository functions.
To my surprise, I do not get any exceptions or errors while the repositories are executed and application.yml has commented connection string.
The result that I get from repository functions is an empty result.
So my question is very simple, why i do not get any indications on a runtime that the connection string is removed does spring create some default connection in case it cannot find the conn string in yml file?
I can't say if there is anything specific with Mongo, but for Spring in general you can create typesafe config and validate it on startup.
application.yml
myapp:
mongo-uri: mongodb://localhost:27017/development
spring:
data:
mongodb:
uri: ${myapp.mongo-uri}
MyAppProperties.java
#Validated
#ConfigurationProperties(prefix = "myapp")
public class MyAppProperties {
#NotEmpty
private String mongoUri;
// accessors
}
In #Configuration's or #SpringBootApplication
#EnableConfigurationProperties(MyAppProperties.class)
The key things are #Validated and #NotEmpty. You will get startup error if ${myapp.mongo-uri} is not provided.
I'm trying to connect to a postgresql instance on google cloud from a spring boot application, using beans.
I created a bean:
#Bean
#Primary
#ConfigurationProperties(prefix = "spring.datasource")
fun createConnectionPool(): DataSource {
val config = HikariConfig()
config.jdbcUrl = String.format("jdbc:postgresql:///%s", DB_NAME)
config.username = DB_USER
config.password = DB_PASS
config.addDataSourceProperty("cloudSqlInstance", INSTANCE_CONNECTION_NAME)
return HikariDataSource(config)
}
and the application.yml file looks like this:
spring:
jp:
features:
hibernate:
jdbc:
lobe:
non_contextual_creation: true
clouds:
gcp:
projectId: my-project-id
SQL:
instance-connection-name: "my-instance"
databaseName: "my-database-name"
but I am getting this error:
could not obtain connection to query metadata","stack_trace":"o.p.u.PSQLException: The server requested password-based authentication, but no password was provided by plugin null\n\t
but I gave it the password :(
I want to mention that I cannot give the password in application.yml
(Because the password comes from an external source and I cannot access it in the application yml file)
What am I doing wrong? Any idea is welcome. thanks
Currently I have a setup like below. On running the batch job locally the job will create the necessary metadata tables automatically using the data-source property values since initialize-schema is set to always. Liquibase will also run and create any tables listed in its changelog.
Here is my application.yml file
spring:
batch:
initialize-schema: always
job:
enabled: true
liquibase:
url: db_url
user: deploy_user
password: deploy_pass
change-log: classpath:db/changelog/db.changelog-master.yaml
enabled: true
data-source:
mysql:
user: r_user
password: r_pass
jdbc-url: db_url
Here is my db.changelog-master.yaml file.
databaseChangeLog:
- changeSet:
dbms: mysql
id: create-sample-table
author: me
sql: CREATE TABLE sample_table (
sample_id VARCHAR(255) NOT NULL,
sample_text TEXT,
PRIMARY KEY (samoke_id)
) ENGINE=InnoDB DEFAULT
CHARSET=utf8 COLLATE=utf8_bin;
Mysql datasource config:
#Configuration
public class DataSourceConfiguration {
#Primary
#Bean(name = "mySQLDataSource")
#ConfigurationProperties("data-source.mysql")
public DataSource mySQLDataSource() {
return DataSourceBuilder.create().type(HikariDataSource.class).build();
}
}
Liquibase Configuration (probably posting more than what's needed):
#Configuration
#EnableConfigurationProperties(LiquibaseProperties.class)
public class LiquibaseConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(LiquibaseConfiguration.class);
#Autowired
private LiquibaseProperties liquibaseProperties;
public DataSource liquibaseDataSource() {
DataSourceBuilder factory = DataSourceBuilder
.create()
.url(liquibaseProperties.getUrl())
.username(liquibaseProperties.getUser())
.password(liquibaseProperties.getPassword());
return factory.build();
}
public void testLiquibaseConnection() throws SQLException {
LOG.info("Testing connection to Liquibase (in case PCF restarts and we have stale dynamic secrets)...");
liquibaseDataSource().getConnection();
LOG.info("Testing connection to Liquibase (in case PCF restarts and we have stale dynamic secrets)... Succeeded");
}
#Bean
public SpringLiquibase liquibase() {
try {
testLiquibaseConnection();
} catch (Exception ex) {
LOG.warn("WARNING: Could not connect to the database using " + liquibaseProperties.getUser() + ", so we will be skipping the Liquibase Migration for now. ", ex);
return null;
}
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog(this.liquibaseProperties.getChangeLog());
liquibase.setContexts(this.liquibaseProperties.getContexts());
liquibase.setDataSource(liquibaseDataSource());
liquibase.setDefaultSchema(this.liquibaseProperties.getDefaultSchema());
liquibase.setDropFirst(this.liquibaseProperties.isDropFirst());
liquibase.setShouldRun(this.liquibaseProperties.isEnabled());
liquibase.setLabels(this.liquibaseProperties.getLabels());
liquibase.setChangeLogParameters(this.liquibaseProperties.getParameters());
return liquibase;
}
}
The issue is we have different credentials for creating/deploying tables and reading/writing to tables in our deployed environments. So the below setup will work to create tables via Liquibase, but fail creating the metadata tables due to having the incorrect credentials upon deployment. Our current work-around to get the metadata tables created is to deploy with the data-source properties having deploy credentials, run the job to initialize the tables and then redeploy with read/write credentials. (We can't just leave the deploy credentials for reads because they have very short TTL).
Is it possible to create the metadata tables for Spring Batch via Liquibase automatically? Specifically, without adding the creation SQL manually to the changelog files?
UPDATE:
Using veljkost's answer below having a changelog file that looks like this works:
databaseChangeLog:
- changeSet:
dbms: mysql
id: create-spring-batch-metadata
author: dev.me
changes:
- sqlFile:
encoding: UTF-8
path: classpath:/org/springframework/batch/core/schema-mysql.sql
relativeToChangelogFile: false
splitStatements: true
stripComments: true
Yes, you can reference the schema files that already exist in Spring Batch project. In org.springframework.batch.core package you can find schema-*.sql files where * is the name of the targeted db. Since you are running on mysql, your change set would look something like this:
- changeSet:
id: 1234
author: adam.sandler
changes:
- sqlFile:
encoding: utf8
path: classpath:/org/springframework/batch/core/schema-mysql.sql
relativeToChangelogFile: false
splitStatements: true
stripComments: true
To auto-migrate to your database without the use of liquabase add
spring.batch.initialize-schema=always
to your application.properties file, it will auto migrate to the embedded data-source
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?
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.
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.
add spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect in application.properties file
I got this error when my database was not created. After creating the DB manually, it worked fine.
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=
Remove the redundant Hibernate Configuration
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.
Add database configuration properties
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
Add Hibernate specific properties
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!
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.
I had same issue. adding this to the application.properties solved the issue:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
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
1. Check if the database is actually installed and its server is started.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
java.net.ConnectException: Connection refused: connect
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
2. Check if the database name is correctly mentioned.
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;
3. Check if the host name and server port is accessible.
org.postgresql.util.PSQLException: Connection to localhost:5431 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
java.net.ConnectException: Connection refused: connect
4. Check if the database credentials are correct.
as #Pankaj mentioned
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
spring.datasource.username= {DB USERNAME HERE}
spring.datasource.password= {DB PASSWORD HERE}
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");
}
}
this is happening because your code is not bale to connect the database. Make sure you have mysql driver and username, password correct.
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
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.
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'.
Make sure you have your database in your pom like OP did. That was my problem.
My problem was that embedded database was already connected. close connection
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.
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
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.
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>
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.
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'#'%';
Adding spring.jpa.database-platform=org.hibernate.dialect.MariaDB53Dialect to my properties file worked for me.
PS: i'm using MariaDB
I reproduced this error message in the following three cases:
There does not exist database user with username written in application.properties file or persistence.properties file or, as in your case, in HibernateConfig file
The deployed database has that user but user is identified by different password than that in one of above files
The database has that user and the passwords match but that user does not have all privileges needed to accomplish all database tasks that your spring-boot app does
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.
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.)
This happened to me because I hadn't added the conf.configure(); before beginning the session:
Configuration conf = new Configuration();
conf.configure();
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.
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.
I faced this issue due to Mysql 8.0.11 version reverting back to 5.7 solved for me
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
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?
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.
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.
add spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQLDialect in application.properties file
I got this error when my database was not created. After creating the DB manually, it worked fine.
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=
Remove the redundant Hibernate Configuration
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.
Add database configuration properties
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
Add Hibernate specific properties
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!
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.
I had same issue. adding this to the application.properties solved the issue:
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
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
1. Check if the database is actually installed and its server is started.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
java.net.ConnectException: Connection refused: connect
org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
2. Check if the database name is correctly mentioned.
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;
3. Check if the host name and server port is accessible.
org.postgresql.util.PSQLException: Connection to localhost:5431 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
java.net.ConnectException: Connection refused: connect
4. Check if the database credentials are correct.
as #Pankaj mentioned
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "postgres"
spring.datasource.username= {DB USERNAME HERE}
spring.datasource.password= {DB PASSWORD HERE}
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");
}
}
this is happening because your code is not bale to connect the database. Make sure you have mysql driver and username, password correct.
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
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.
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'.
Make sure you have your database in your pom like OP did. That was my problem.
My problem was that embedded database was already connected. close connection
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.
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
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.
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>
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.
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'#'%';
Adding spring.jpa.database-platform=org.hibernate.dialect.MariaDB53Dialect to my properties file worked for me.
PS: i'm using MariaDB
I reproduced this error message in the following three cases:
There does not exist database user with username written in application.properties file or persistence.properties file or, as in your case, in HibernateConfig file
The deployed database has that user but user is identified by different password than that in one of above files
The database has that user and the passwords match but that user does not have all privileges needed to accomplish all database tasks that your spring-boot app does
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.
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.)
This happened to me because I hadn't added the conf.configure(); before beginning the session:
Configuration conf = new Configuration();
conf.configure();
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.
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.
I faced this issue due to Mysql 8.0.11 version reverting back to 5.7 solved for me
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