简体   繁体   中英

Unable to connect to MySQL database from SpringBoot

I am trying to connect to the mySQL database from my Spring Boot application. However it is showing error when I am trying to run it.

How can I resolve this?

The error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory

Adding code snippets from my files

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>io.nkamanoo.springboot</groupId>
  <artifactId>course-api</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Java Brains Course API</name>



<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>

<dependencies>

    <dependency>
       <groupId> org.springframework.boot </groupId>
       <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
            <version>8.0.19</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
</dependencies>


 <properties>
    <java.version>1.8</java.version>
 </properties>

 </project>

application.properties

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/db_example
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.hibernate.use-new-id-generator-mappings=false
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

stack trace

2020-02-10 13:44:34.073  INFO 63618 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.0.12.Final}
2020-02-10 13:44:34.075  INFO 63618 --- [           main] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2020-02-10 13:44:34.078  INFO 63618 --- [           main] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2020-02-10 13:44:34.139  INFO 63618 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
2020-02-10 13:44:34.295  INFO 63618 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2020-02-10 13:44:34.823  INFO 63618 --- [           main] org.hibernate.tool.hbm2ddl.SchemaUpdate  : HHH000228: Running hbm2ddl schema update
2020-02-10 13:44:34.864  INFO 63618 --- [           main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: topic
2020-02-10 13:44:34.867  INFO 63618 --- [           main] rmationExtractorJdbcDatabaseMetaDataImpl : HHH000262: Table not found: topic
2020-02-10 13:44:34.886  WARN 63618 --- [           main] ationConfigEmbeddedWebApplicationContext : 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/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
2020-02-10 13:44:34.892  INFO 63618 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-02-10 13:44:34.910  INFO 63618 --- [           main] utoConfigurationReportLoggingInitializer : 

I havent created the tables manually in sql as i thought spring.jpa.hibernate.ddl-auto=update should do it

It can be related to bad @annotations . Post your entity class to re-check. Anyhow add the below line in your property file:

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM