0

I am new to JPA and Springboot. I checked and tried solutions from several posts here, however, could not really solve my issue.

When I try to use JPA to connect with my mysql database, it throws Unknown Database 'springsecurity'. I verified my username and password for the db.

I am using mysqlworkbench as rdbms.

application.properties :

spring.datasource.url=jdbc:mysql://localhost:3306/springsecuritysystem
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

Error thrown :

2021-11-11 23:39:21.993  WARN 72026 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1049, SQLState: 42000
2021-11-11 23:39:21.993 ERROR 72026 --- [           main] o.h.engine.jdbc.spi.SqlExceptionHelper   : Unknown database 'springsecuritysystem'
2021-11-11 23:39:21.999 ERROR 72026 --- [           main] j.LocalContainerEntityManagerFactoryBean : Failed to initialize JPA EntityManagerFactory: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.SQLGrammarException: Unable to open JDBC Connection for DDL execution
2021-11-11 23:39:22.000  WARN 72026 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.exception.SQLGrammarException: Unable to open JDBC Connection for DDL execution
2021-11-11 23:39:22.001  INFO 72026 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]

Tutorial followed : https://github.com/koushikkothagal/spring-security-jpa

rattlehead
  • 57
  • 9
  • 2
    have you created the database/schema (`springsecuritysystem`)? no -> https://stackoverflow.com/q/5515745/592355 – xerx593 Nov 11 '21 at 23:54
  • 1
    also related/duplicate: https://stackoverflow.com/q/19678769/592355 – xerx593 Nov 11 '21 at 23:56
  • 1
    I did not create it explicitly thinking springframework will create it by itself, like it does for apache derby. Does it mean, spring doesn't create it for mysql ? – rattlehead Nov 12 '21 at 01:36
  • 1
    thank you so much ! that solution worked. – rattlehead Nov 12 '21 at 01:46
  • 1
    cool (you got it)! :-) i deleted the comment because thought it was outdated/wrong ;D;D here the links: https://stackoverflow.com/a/54336968/592355 , and https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.data.spring.jpa.properties – xerx593 Nov 12 '21 at 01:51

1 Answers1

0

Try edit this application.properties:

spring.datasource.url = jdbc:mysql://localhost:3306/springsecuritysystem?createDatabaseIfNotExist=true

There is no existing "springsecuritysystem" schema on mySQL. ?createDatabaseIfNotExist=true this command help's to check if its not exist then it will create a new schema on mySQL