【问题标题】:Error creating bean with name 'entityManagerFactory' defined in class path resource: rror creating bean with name 'entityManagerFactory'创建类路径资源中定义的名称为“entityManagerFactory”的bean时出错:创建名称为“entityManagerFactory”的bean时出错
【发布时间】:2021-04-02 15:42:30
【问题描述】:

我创建了一个 Spring 应用程序,并使用 hibernate 进行逆向工程,从 MySQL Db 生成 java 类。 之后,我想使用这个类来实现存储库,但是我遇到了这个问题:

org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义名称为“entityManagerFactory”的 bean 创建错误: init 方法调用失败;嵌套异常是 java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException

这是我的 build.gradle 文件

plugins {
    id 'org.springframework.boot' version '2.4.4'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    
    id "org.hibernate.gradle.tools" version "1.2.5"
        
}

group = 'it.unisalento'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {

    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    developmentOnly 'org.springframework.boot:spring-boot-devtools'
    runtimeOnly 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-validation'
    implementation 'org.javassist:javassist:3.27.0-GA'
    
    
    compile 'org.hibernate:hibernate-core:5.2.11.Final'
    compile 'org.hibernate:hibernate-entitymanager:5.2.11.Final'
    compile 'mysql:mysql-connector-java'
    compile 'org.apache.commons:commons-dbcp2:2.0.1'
    
    
}

test {
    useJUnitPlatform()
}

import org.hibernate.gradle.tools.*

 

database{
    catalog =  ["myalertdb": new Schema()]
    basePackage = "it.unisalento.myalertapp.app.domain"
    port = 3306
    url = "jdbc:mysql://127.0.0.1/myalertdb?serverTimezone=GMT"
    user = "root"
    password = "database123"
    dialect = "org.hibernate.dialect.MySQLDialect"
    driver = "com.mysql.cj.jdbc.Driver"
}

【问题讨论】:

    标签: linux spring hibernate


    【解决方案1】:

    看到这种情况的一个原因是您正在运行 java 9 或更高版本。然后你需要将jaxb-api.jar 添加到你的类路径中。如果您正在运行 maven,您可以在 pom.xml 中添加以下内容:

    <dependency>
        <groupId>javax.xml.bind</groupId>
         <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
     </dependency>
    
    

    请参阅https://www.jesperdj.com/2018/09/30/jaxb-on-java-9-10-11-and-beyond/了解更多详情。

    【讨论】:

      猜你喜欢
      • 2020-03-25
      • 1970-01-01
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 2015-05-10
      • 1970-01-01
      • 2015-10-16
      相关资源
      最近更新 更多