I have got this access denied exception :
java.sql.SQLException: Access denied for user ''#'localhost' (using
password: NO)
org.springframework.jdbc.support.MetaDataAccessException: Could not get Connection for extracting meta-data; nested exception is
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to
obtain JDBC Connection; nested exception is java.sql.SQLException:
Access denied for user ''#'localhost' (using password: NO)
018-08-23 00:55:21.791 INFO 1260 --- [ main] j.LocalContainerEntityManagerFactoryBean : Building JPA container
EntityManagerFactory for persistence unit 'default' 2018-08-23
00:55:21.813 INFO 1260 --- [ main]
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing
PersistenceUnitInfo [ name: default ...] 2018-08-23 00:55:22.180
INFO 1260 --- [ main] org.hibernate.Version
: HHH000412: Hibernate Core {5.2.17.Final} 2018-08-23 00:55:22.181
INFO 1260 --- [ main] org.hibernate.cfg.Environment
: HHH000206: hibernate.properties not found 2018-08-23 00:55:22.221
INFO 1260 --- [ main] o.hibernate.annotations.common.Version
: HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-08-23
00:55:22.351 INFO 1260 --- [ main]
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2018-08-23 00:55:23.445 ERROR 1260 --- [ main]
com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception
during pool initialization.
application properties file:
spring.thymeleaf.cache=false
#===============================
#DATA SOURCE
#===============================
#set here configurations for the database connection
spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredatabase?verifyServerCertificate=false&useSSL=true
#username and secret
spring.datasource.data.username=root
spring.datasource.data.password=
#keep connection alive if idle for long time
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1
#===================
#jpa/hibernate
#===================
#use spring.jpa.properties.* for hibernate native properties
#stripped before adding them to the entity manager
#show or not log for each sql query
spring.jpa.show-sql=true
#jdbc driver class
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#the project
spring.jpa.hibernate.ddl-auto=create
#allows hibernate to generat sql optimised for particular dbms
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
my java file:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class BookstoreApplication {
public static void main(String[] args) {
SpringApplication.run(BookstoreApplication.class, args);
}
}
It seems that your database username and password are not set with correct property names, as in spring-boot the name of the properties are predefined and only those should be used while giving configuration information. Just try below property keys for username and password :
spring.datasource.username =
spring.datasource.password =
Provide appropriate values..
java.sql.SQLException: Access denied for user ''#'localhost' (using password: NO)
It seems your user password is incorrect. Try to login MySQL with same credentials and it will display the same error.
Related
I am trying to run a basic spring boot application with JPA by using spanner emulator. However, I am getting the below error after startup after hitting any exposed end point.
PERMISSION_DENIED: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Caller is missing IAM permission spanner.sessions.create on resource projects/test-project/instances/test-instance/databases/test-database.
Project has been cloned from https://github.com/GoogleCloudPlatform/google-cloud-spanner-hibernate/tree/master/google-cloud-spanner-hibernate-samples/spring-data-jpa-sample
and the only change I have done is updated the application.properties file.
as per my understanding spanner emulator should not have any IAM related issues as it doesn't need any. I am not sure what is causing this issue here. I have verified that the emulator config is the active config and hence I would expect the code to connect to emulator.
# Application configuration to use Cloud Spanner with Spring Data JPA
# Spanner connection URL.
# - ${PROJECT_ID} Replace with your GCP project ID
# - ${INSTANCE_ID} Replace with your Spanner instance ID
# - ${DATABASE_NAME} Replace with your Spanner database name within your Spanner instance
spring.datasource.url=jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database
# Specify the Spanner JDBC driver.
spring.datasource.driver-class-name=com.google.cloud.spanner.jdbc.JdbcDriver
# Specify the Spanner Hibernate dialect.
spring.jpa.properties.hibernate.dialect=com.google.cloud.spanner.hibernate.SpannerDialect
#spring.jpa.hibernate.ddl-auto=update //same error even if I uncomment this line
# Settings to enable batching statements for efficiency
spring.jpa.properties.hibernate.jdbc.batch_size=100
# You may display SQL statements and stats for debugging if needed.
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
Please find the console logs below:
2021-10-06 12:59:12.549 INFO 9747 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-10-06 12:59:12.556 INFO 9747 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-10-06 12:59:12.556 INFO 9747 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.53]
2021-10-06 12:59:12.617 INFO 9747 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-10-06 12:59:12.617 INFO 9747 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 694 ms
2021-10-06 12:59:12.707 INFO 9747 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2021-10-06 12:59:12.737 WARN 9747 --- [ main] c.g.a.oauth2.DefaultCredentialsProvider : Your application has authenticated using end user credentials from Google Cloud SDK. We recommend that most server applications use service accounts instead. If your application continues to use end user credentials from Cloud SDK, you might receive a "quota exceeded" or "API not enabled" error. For more information about service accounts, see https://cloud.google.com/docs/authentication/.
2021-10-06 12:59:14.558 INFO 9747 --- [ main] com.zaxxer.hikari.pool.PoolBase : HikariPool-1 - Driver does not support get/set network timeout for connections. (Network timeout is not supported)
2021-10-06 12:59:22.165 INFO 9747 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2021-10-06 12:59:22.197 INFO 9747 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2021-10-06 12:59:22.231 INFO 9747 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.29.Final
2021-10-06 12:59:22.337 INFO 9747 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-10-06 12:59:22.396 INFO 9747 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: com.google.cloud.spanner.hibernate.SpannerDialect
2021-10-06 12:59:22.851 INFO 9747 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2021-10-06 12:59:22.857 INFO 9747 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2021-10-06 12:59:23.060 WARN 9747 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2021-10-06 12:59:23.184 INFO 9747 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index
2021-10-06 12:59:23.290 INFO 9747 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-10-06 12:59:23.298 INFO 9747 --- [ main] com.example.CoffeeApplication : Started CoffeeApplication in 12.957 seconds (JVM running for 13.227)
2021-10-06 12:59:33.646 INFO 9747 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-10-06 12:59:33.646 INFO 9747 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-10-06 12:59:33.647 INFO 9747 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
Hibernate:
select
customer0_.id as id1_1_,
customer0_.email as email2_1_,
customer0_.name as name3_1_
from
customer customer0_
2021-10-06 12:59:35.650 WARN 9747 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 7, SQLState: null
2021-10-06 12:59:35.650 ERROR 9747 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : PERMISSION_DENIED: com.google.api.gax.rpc.PermissionDeniedException: io.grpc.StatusRuntimeException: PERMISSION_DENIED: Caller is missing IAM permission spanner.sessions.create on resource projects/test-project/instances/test-instance/databases/test-database.
2021-10-06 12:59:35.668 ERROR 9747 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: could not extract ResultSet; nested exception is org.hibernate.exception.GenericJDBCException: could not extract ResultSet] with root cause
io.grpc.StatusRuntimeException: PERMISSION_DENIED: Caller is missing IAM permission spanner.sessions.create on resource projects/test-project/instances/test-instance/databases/test-database.
at io.grpc.Status.asRuntimeException(Status.java:535) ~[grpc-api-1.40.1.jar:1.40.1]
at io.grpc.stub.ClientCalls$UnaryStreamToFuture.onClose(ClientCalls.java:533) ~[grpc-stub-1.40.1.jar:1.40.1]
at io.grpc.PartialForwardingClientCallListener.onClose(PartialForwardingClientCallListener.java:39) ~[grpc-api-1.40.1.jar:1.40.1]
at io.grpc.ForwardingClientCallListener.onClose(ForwardingClientCallListener.java:23) ~[grpc-api-1.40.1.jar:1.40.1]
at io.grpc.ForwardingClientCallListener$SimpleForwardingClientCallListener.onClose(ForwardingClientCallListener.java:40) ~[grpc-api-1.40.1.jar:1.40.1]
at com.google.cloud.spanner.spi.v1.SpannerErrorInterceptor$1$1.onClose(SpannerErrorInterceptor.java:100) ~[google-cloud-spanner-6.12.5.jar:6.12.5]
at io.grpc.internal.ClientCallImpl.closeObserver(ClientCallImpl.java:557) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ClientCallImpl.access$300(ClientCallImpl.java:69) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInternal(ClientCallImpl.java:738) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ClientCallImpl$ClientStreamListenerImpl$1StreamClosed.runInContext(ClientCallImpl.java:717) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.ContextRunnable.run(ContextRunnable.java:37) ~[grpc-core-1.40.1.jar:1.40.1]
at io.grpc.internal.SerializingExecutor.run(SerializingExecutor.java:133) ~[grpc-core-1.40.1.jar:1.40.1]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) ~[na:1.8.0_281]
at java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_281]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) ~[na:1.8.0_281]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) ~[na:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_281]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_281]
The error is a clear indication that your application is not trying to connect to the emulator, but to a real Spanner database. In order to connect to the emulator instead of 'real' Cloud Spanner, you should do one of the following:
Set the environment variable SPANNER_EMULATOR_HOST=localhost:9010
OR: Edit the JDBC connection URL to include the autoConfigEmulator=true property. Your JDBC URL would then become jdbc:cloudspanner:/projects/test-project/instances/test-instance/databases/test-database;autoConfigEmulator=true
The advantage of the second alternative is that the autoConfigEmulator property will not only ensure that the JDBC driver is connecting to the emulator, it will also automatically create the test-instance and test-database on the emulator if they do not already exist.
I am using spring boot 2.5.3 and postgreSQL for backend.
I want this app to run on localhost:5000/api/v1/customers as well as from docker-compose.
application.properties:
server.port=5000
#Database setup
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/customers
spring.datasource.username=postgres
spring.datasource.password=postgres
This works with localhost, with mvn spring-boot:run
But, when I use the following application.properties settings from localhost to db
server.port=5000
#Database setup
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://db:5432/customers
spring.datasource.username=postgres
spring.datasource.password=postgres
Dockerfile
FROM maven:3.8.1-openjdk-11-slim
COPY ./target/customer-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
docker-compose.yml
version: '3.1'
services:
app:
container_name: customer-container
image: customer-image:v1
build: ./
ports:
- "5000:5000"
depends_on:
- db
db:
image: postgres
ports:
- "5432:5432"
environment:
- POSTGRES_PASSWORD=postgres
- POSTGRES_USER=postgres
- POSTGRES_DB=customers
I cannot connect to localhost:5000/api/v1/customers after mvn spring-boot:run
Error:
org.postgresql.util.PSQLException: Connection to db:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:303) ~[postgresql-42.2.23.jar:42.2.23]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:51) ~[postgresql-42.2.23.jar:42.2.23]
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223) ~[postgresql-42.2.23.jar:42.2.23]
at org.postgresql.Driver.makeConnection(Driver.java:465) ~[postgresql-42.2.23.jar:42.2.23]
at org.postgresql.Driver.connect(Driver.java:264) ~[postgresql-42.2.23.jar:42.2.23]
Tried to create a docker image docker build -t customer-image:v1 .
, and run docker-compose build docker-compose up there are no error.
Logs says it is running on port 5000.
customer-container | 2021-08-29 12:43:42.053 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 5000 (http)
customer-container | 2021-08-29 12:43:42.068 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
customer-container | 2021-08-29 12:43:42.069 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
customer-container | 2021-08-29 12:43:42.189 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
customer-container | 2021-08-29 12:43:42.190 INFO 1 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1876 ms
customer-container | 2021-08-29 12:43:42.487 INFO 1 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
customer-container | 2021-08-29 12:43:42.567 INFO 1 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.4.32.Final
customer-container | 2021-08-29 12:43:42.702 INFO 1 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
customer-container | 2021-08-29 12:43:42.827 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
customer-container | 2021-08-29 12:43:42.970 INFO 1 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
customer-container | 2021-08-29 12:43:42.995 INFO 1 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
customer-container | 2021-08-29 12:43:43.631 WARN 1 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Warning Code: 0, SQLState: 00000
customer-container | 2021-08-29 12:43:43.631 WARN 1 --- [ main] o.h.engine.jdbc.spi.SqlExceptionHelper : table "customer" does not exist, skipping
customer-container | 2021-08-29 12:43:43.642 INFO 1 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
customer-container | 2021-08-29 12:43:43.656 INFO 1 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
customer-container | 2021-08-29 12:43:44.252 WARN 1 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
customer-container | 2021-08-29 12:43:44.991 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 5000 (http) with context path ''
customer-container | 2021-08-29 12:43:45.290 INFO 1 --- [ main] c.p.customer.CustomerApplication : Started CustomerApplication in 5.654 seconds (JVM running for 6.531)
This works localhost:5000/api/v1/customers when docker-compose is up.
Is there a way we can have condtional name for host in this line?Is there any other way?
spring.datasource.url=jdbc:postgresql://db:5432/customers
Since the host development environment and the container runtime environment are different, you can use environment variables to differentiate between them. Spring Boot knows how to set Spring properties from environment variables. So I'd recommend:
In your application.properties file, set spring.datasource.url to the localhost URL you'd use in development.
In your Compose setup, set the SPRING_DATASOURCE_URL environment variable to the name specific to that Compose setup
version: '3.8'
services:
db: { ... }
app:
environment:
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/customers
...
Do not create a separate Spring profile per environment; you would not want to recompile your jar file if you changed the database name or password, or deployed this in Kubernetes, or used a cloud-hosted database like Amazon RDS, or...
So, you want to be able to run both locally and from docker?
If that's the case, you need to extract the database connection string to an environment variable that you set at runtime
spring.datasource.url = ${DB_HOST}:${DB_PORT}/
spring.datasource.username = ${DB_USERNAME}
spring.datasource.password = ${DB_PASSWD}
Or you can introduce two different property files that get set from a Spring profile, which you also would set at runtime; Refer SPRING_PROFILES_ACTIVE environment variable
Based on what I see, you are not supposed to use localhost on spring.datasource.url because your docker is within the container itself, you should be using
jdbc:postgresql://host.docker.internal:5432/customers instead. For more details, you can view from this link https://docs.docker.com/desktop/windows/networking/#i-cannot-ping-my-containers
Is there a way we can have condtional name for host in this line?Is there any other way?
As for this, it would be prefered to set it as an environmetn variable on your docker compose file and connect using dbconn url
If you are using linux as your operating system on which you are running your docker , you can set this property
network_mode: host
on both the containers, this will attach your container's port to your localhost port.
You will need to remove the port forwarding config for this to work.
I am trying to execute the following SQL via schema.sql as my Spring Boot application starts:
create table tbl_user(
id int auto_increment, name varchar2(50), primary key(id)
);
When I try to run an insert statement against this with H2-console I get:
NULL not allowed for column "ID"; SQL statement:
insert into tbl_user(name) values ('Bob') [23502-200] 23502/23502
However, if I drop the table and run the same creation and insertion using H2-console the insert succeeds without an error.
2021-01-30 20:17:24.325 INFO 38184 --- [ main] o.hibernate.jpa.internal.util.LogHelper :
HHH000204: Processing PersistenceUnitInfo [name: default]
2021-01-30 20:17:24.380 INFO 38184 --- [ main] org.hibernate.Version :
HHH000412: Hibernate ORM core version 5.4.27.Final
2021-01-30 20:17:24.543 INFO 38184 --- [ main] o.hibernate.annotations.common.Version :
HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2021-01-30 20:17:24.646 INFO 38184 --- [ main] org.hibernate.dialect.Dialect :
HHH000400: Using dialect: org.hibernate.dialect.H2Dialect
2021-01-30 20:17:25.129 INFO 38184 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator :
HHH000490: Using JtaPlatform implementation:
[org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=h2user
spring.datasource.password=h2password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.format_sql=true
spring.h2.console.enabled=true
I suggest you adding spring.jpa.hibernate.ddl-auto=none to avoid H2 database auto-creation done by spring boot.
You can read the post at Spring boot with H2 Database auto create and the spring boot documentation at https://docs.spring.io/spring-boot/docs/2.4.x/reference/htmlsingle/#howto-database-initialization.
I'm having a problem with spring boot and hibernate.
To initialize is taking too long as you can see in the log (+20 minutes).
How can I have more information from log to know what the spring/hibernate is doing between those two steps?
Thank you
2020-03-23 15:27:16.199 INFO 6268 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2020-03-23 15:27:16.223 INFO 6268 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
2020-03-23 16:06:19.187 INFO 6268 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
Until now not solved.
I created a SpringBoot 1.5.11.RELEASE and Hibernate 5 project. Everything was working fine.
However, when I changed SpringBoot version to 2.0.0.RELEASE the spring.jpa.hibernate.ddl-auto=create does not work. I have tried this stackoverflow post, but no luck.
Here is the stack trace of SpringBoot 1.5.11.RELEASE :
2018-04-06 16:27:52.512 INFO 6792 --- [ main]
j.LocalContainerEntityManagerFactoryBean : Building JPA container
EntityManagerFactory for persistence unit 'default' 2018-04-06
16:27:52.526 INFO 6792 --- [ main]
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing
PersistenceUnitInfo [ name: default ...] 2018-04-06 16:27:52.587
INFO 6792 --- [ main] org.hibernate.Version
: HHH000412: Hibernate Core {5.0.12.Final} 2018-04-06 16:27:52.589
INFO 6792 --- [ main] org.hibernate.cfg.Environment
: HHH000206: hibernate.properties not found 2018-04-06 16:27:52.591
INFO 6792 --- [ main] org.hibernate.cfg.Environment
: HHH000021: Bytecode provider name : javassist 2018-04-06
16:27:52.735 INFO 6792 --- [ main]
o.hibernate.annotations.common.Version : HCANN000001: Hibernate
Commons Annotations {5.0.1.Final} 2018-04-06 16:27:52.833 INFO 6792
--- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2018-04-06 16:27:53.180 INFO 6792 --- [ main]
org.hibernate.tool.hbm2ddl.SchemaExport : HHH000227: Running hbm2ddl
schema export Hibernate: drop table if exists person Hibernate: create
table person (id integer not null auto_increment, name varchar(255),
primary key (id)) 2018-04-06 16:27:53.619 INFO 6792 --- [
main] org.hibernate.tool.hbm2ddl.SchemaExport : HHH000230: Schema
export complete 2018-04-06 16:27:53.646 INFO 6792 --- [
main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA
EntityManagerFactory for persistence unit 'default' SessionFactory
Created 2018-04-06 16:27:54.528 WARN 6792 --- [ main]
org.hibernate.orm.connections : HHH10001002: Using
Hibernate built-in connection pool (not for production use!)
2018-04-06 16:27:54.529 INFO 6792 --- [ main]
org.hibernate.orm.connections : HHH10001005: using driver
[com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hotel]
2018-04-06 16:27:54.529 INFO 6792 --- [ main]
org.hibernate.orm.connections : HHH10001001: Connection
properties: {user=root, password=****} 2018-04-06 16:27:54.529 INFO
6792 --- [ main] org.hibernate.orm.connections :
HHH10001003: Autocommit mode: false 2018-04-06 16:27:54.532 INFO 6792
--- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 10 (min=1) 2018-04-06
16:27:54.539 INFO 6792 --- [ main]
org.hibernate.dialect.Dialect : HHH000400: Using dialect:
org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:27:54.855 INFO 6792
--- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup Hibernate: insert into
person (name) values (?) Saved!! 2018-04-06 16:27:54.954 INFO 6792
--- [ main] com.hib.Hibernate5Application : Started Hibernate5Application in 4.051 seconds (JVM running for 4.802)
Here is the stack trace of SpringBoot 2.0.0.RELEASE :
SessionFactory Created 2018-04-06 16:24:17.011 INFO 5092 --- [
main] org.hibernate.Version : HHH000412: Hibernate
Core {5.2.14.Final} 2018-04-06 16:24:17.013 INFO 5092 --- [
main] org.hibernate.cfg.Environment : HHH000206:
hibernate.properties not found 2018-04-06 16:24:17.915 INFO 5092 ---
[ main] o.hibernate.annotations.common.Version :
HCANN000001: Hibernate Commons Annotations {5.0.1.Final} 2018-04-06
16:24:17.989 WARN 5092 --- [ main]
org.hibernate.orm.connections.pooling : HHH10001002: Using
Hibernate built-in connection pool (not for production use!)
2018-04-06 16:24:17.994 INFO 5092 --- [ main]
org.hibernate.orm.connections.pooling : HHH10001005: using driver
[com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hotel]
2018-04-06 16:24:17.994 INFO 5092 --- [ main]
org.hibernate.orm.connections.pooling : HHH10001001: Connection
properties: {user=root, password=****} 2018-04-06 16:24:17.994 INFO
5092 --- [ main] org.hibernate.orm.connections.pooling :
HHH10001003: Autocommit mode: false 2018-04-06 16:24:17.996 INFO 5092
--- [ main] .c.i.DriverManagerConnectionProviderImpl : HHH000115: Hibernate connection pool size: 10 (min=1)
2018-04-06 16:24:18.230 INFO 5092 --- [ main]
org.hibernate.dialect.Dialect : HHH000400: Using dialect:
org.hibernate.dialect.MySQL5Dialect 2018-04-06 16:24:19.041 INFO 5092
--- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting... 2018-04-06 16:24:19.058 INFO 5092 --- [
main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start
completed.
2018-04-06 16:24:19.419 INFO 5092 --- [ main]
o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX
exposure on startup 2018-04-06 16:24:19.420 INFO 5092 --- [
main] o.s.j.e.a.AnnotationMBeanExporter : Bean with name
'dataSource' has been autodetected for JMX exposure 2018-04-06
16:24:19.427 INFO 5092 --- [ main]
o.s.j.e.a.AnnotationMBeanExporter : Located MBean 'dataSource':
registering with JMX server as MBean
[com.zaxxer.hikari:name=dataSource,type=HikariDataSource] 2018-04-06
16:24:19.441 INFO 5092 --- [ main]
com.hib.Hibernate5Application : Started
Hibernate5Application in 3.742 seconds (JVM running for 4.541)
Hibernate: insert into person (name) values (?) Saved!!
Hibernate 5 configuration :
#Configuration
public class HinernateConfig {
private StandardServiceRegistry standardServiceRegistry;
private SessionFactory sessionFactory;
#Bean
public SessionFactory sessionFactory() {
try {
standardServiceRegistry = new StandardServiceRegistryBuilder().configure().build();
Metadata metadata = new MetadataSources(standardServiceRegistry).getMetadataBuilder().build();
sessionFactory = metadata.getSessionFactoryBuilder().build();
return sessionFactory;
} catch (Exception e) {
System.out.println("Exception occured while creating SessionFactory");
e.printStackTrace();
if (standardServiceRegistry != null) {
StandardServiceRegistryBuilder.destroy(standardServiceRegistry);
}
}
return sessionFactory;
}
#PostConstruct
public void post() {
System.out.println("SessionFactory Created");
}
}
Main application :
#SpringBootApplication
public class Application implements CommandLineRunner {
private SessionFactory sessionFactory;
public Application(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
#Override
public void run(String... args) throws Exception {
Session session = sessionFactory.openSession();
session.beginTransaction();
Person person = new Person("Mehraj Malik");
session.save(person);
System.out.println("Saved!!");
session.getTransaction().commit();
session.close();
}
}
Hibernate Xml config
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hotel</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<mapping class="com.hib.entity.Person"/>
</session-factory>
</hibernate-configuration>
application.properties
spring.datasource.url = jdbc:mysql://localhost:3306/Hotel
spring.datasource.username=root
spring.datasource.password=admin
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
In Boot 2.0 stack trace the below part does not appear :
Running hbm2ddl schema export Hibernate: drop table if exists person
Hibernate: create table person (id integer not null auto_increment,
name varchar(255), primary key (id))
Github : https://github.com/Mehraj123/Hib5Boot2.0
As mentioned by M.Deinum I've deleted the following classes and resource file and it works :
HinernateConfig
hibernate.cfg.xml
And changes in Main application
#SpringBootApplication
public class Hibernate5Application implements CommandLineRunner {
private EntityManagerFactory managerFactory;
public Hibernate5Application(EntityManagerFactory managerFactory) {
this.managerFactory = managerFactory;
}
public static void main(String[] args) {
SpringApplication.run(Hibernate5Application.class, args);
}
#Override
#Transactional
public void run(String... args) throws Exception {
EntityManager entityManager = managerFactory.createEntityManager();
entityManager.getTransaction().begin();
Person person = new Person("Mehraj Malik");
entityManager.persist(person);
System.out.println("Saved!!");
entityManager.close();
}
}
I've updated my github repo