创建类路径资源中定义的名为“entityManagerFactory”的 bean 时出错

当我尝试使用 hibernate 和 MySql 运行我的 spring-boot 项目时,出现错误。

我的应用程序.properties


spring.datasource.url = jdbc:mysql://localhost:3306/spring-mysql?useSSL=false

spring.datasource.username = root

spring.datasource.password = 


spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

spring.jpa.hibernate.ddl-auto = update


在 pom.xml 中,我有以下依赖项,


<parent>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-parent</artifactId>

        <version>2.1.6.RELEASE</version>

        <relativePath /> <!-- lookup parent from repository -->

    </parent>


    <properties>

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

    </properties>


    <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>org.springframework.boot</groupId>

            <artifactId>spring-boot-devtools</artifactId>

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>mysql</groupId>

            <artifactId>mysql-connector-java</artifactId>

            <scope>runtime</scope>

        </dependency>

        <dependency>

            <groupId>org.springframework.boot</groupId>

            <artifactId>spring-boot-starter-test</artifactId>

            <scope>test</scope>

        </dependency>

    </dependencies>


我还提到了以前关于此问题的问题(创建名为“entityManagerFactory”的 bean 在类路径资源中定义的错误:调用 init 方法失败),但没有任何帮助。

完整的代码库可在https://github.com/tenusha/application-frameworks/tree/master/spring-boot-mysql



慕少森
浏览 136回答 6
6回答

holdtom

您使用了错误包中的 @Id 注释:import&nbsp;org.springframework.data.annotation.Id;它一定要是:&nbsp;import&nbsp;javax.persistence.Id;

茅侃侃

添加@Id annotation所有模态类(Course, Subject)&nbsp;确保导入的包是import javax.persistence.Id;

料青山看我应如是

前几天我收到错误消息,比如需要一个名为“entityManagerFactory”的 bean,但找不到。经过大量谷歌搜索解决了这个问题。我为 JPA 设置了手动配置。@Bean&nbsp;public LocalSessionFactoryBean sessionFactory() {&nbsp; &nbsp; LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();&nbsp;&nbsp; &nbsp; return sessionFactory;}但 JPA 默认按名称“entityManagerFactory”搜索 sessionFactory,因此将我的代码更改为:@Bean(name="entityManagerFactory")public LocalSessionFactoryBean sessionFactory() {&nbsp; &nbsp; LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();&nbsp; &nbsp; return sessionFactory;}&nbsp;我之前的回答更新 导入正确的类import javax.persistence.Id;您尝试在没有外键参考的情况下加入学科课程...根据该图像更改您的代码

ibeautiful

我遇到了同样的问题,我通过将版本添加到 pom.xml 中的 javax persistence api 来修复它,更改它:&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.persistence</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javax.persistence-api</artifactId>&nbsp; &nbsp; </dependency>为此:&nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.persistence</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>javax.persistence-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.2</version>&nbsp; &nbsp; </dependency>

慕容708150

添加@EntityScan(basePackages = "io.summer")到您的应用程序类。这是休眠扫描您的实体所必需的。(为我工作,为 postgresql)package com.sliit.af;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.data.jpa.repository.config.EnableJpaAuditing;@SpringBootApplication@EnableJpaAuditing@EntityScan(basePackages = "com.sliit.af")&nbsp; &nbsp; &nbsp; &nbsp;/* ADDED HERE */public class SpringBootMysqlApplication {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; SpringApplication.run(SpringBootMysqlApplication.class, args);&nbsp; &nbsp; }}

开满天机

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"&nbsp; &nbsp; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&nbsp; &nbsp; xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">&nbsp; &nbsp; <modelVersion>4.0.0</modelVersion>&nbsp; &nbsp; <parent>&nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-parent</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; <version>2.3.1.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; <relativePath />&nbsp; &nbsp; &nbsp; &nbsp; <!-- lookup parent from repository -->&nbsp; &nbsp; </parent>&nbsp; &nbsp; <groupId>com.revature</groupId>&nbsp; &nbsp; <artifactId>restful-web-services</artifactId>&nbsp; &nbsp; <version>0.0.1-SNAPSHOT</version>&nbsp; &nbsp; <name>RestfulWebServices</name>&nbsp; &nbsp; <description>Restful Web Services project for Spring Boot</description>&nbsp; &nbsp; <properties>&nbsp; &nbsp; &nbsp; &nbsp; <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>&nbsp; &nbsp; &nbsp; &nbsp; <java.version>1.8</java.version>&nbsp; &nbsp; &nbsp; &nbsp; <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>&nbsp; &nbsp; </properties>&nbsp; &nbsp; <dependencies>&nbsp; &nbsp; &nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-data-jpa</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.3.3.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->&nbsp; &nbsp; &nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-jdbc</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.3.3.RELEASE</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.h2database</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>h2</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>runtime</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.postgresql</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>postgresql</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>42.2.5</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.slf4j</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>slf4j-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.7.25</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-web</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependency> -->&nbsp; &nbsp; &nbsp; &nbsp; <!-- START AND STOP THE APPLICATION AFTER MAKING THE CHANGE! -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-validation</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!--<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependency> -->&nbsp; &nbsp; &nbsp; &nbsp; <!-- provides information concerning the health metrics etc. of applicaiton -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-actuator</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- provides view of data and consumes info from actuator -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.data</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-data-rest-hal-browser</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-hateoas</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>com.fasterxml.jackson.dataformat</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jackson-dataformat-xml</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>io.springfox</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>springfox-boot-starter</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.0.0</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>io.springfox</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>springfox-boot-starter</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>3.0.0</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId>&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </dependency> -->&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-devtools</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>runtime</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>javax.xml.bind</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>jaxb-api</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>2.3.0</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>junit</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>junit</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>4.9</version>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.mockito</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>mockito-core</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <version>1.9.5</version>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; &nbsp; &nbsp; <dependency>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-starter-test</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <scope>test</scope>&nbsp; &nbsp; &nbsp; &nbsp; </dependency>&nbsp; &nbsp; </dependencies>&nbsp; &nbsp; <build>&nbsp; &nbsp; &nbsp; &nbsp; <plugins>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <plugin>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <groupId>org.springframework.boot</groupId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <artifactId>spring-boot-maven-plugin</artifactId>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </plugin>&nbsp; &nbsp; &nbsp; &nbsp; </plugins>&nbsp; &nbsp; </build>&nbsp; &nbsp; <repositories>&nbsp; &nbsp; &nbsp; &nbsp; <repository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>spring-snapshots</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>Spring Snapshots</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>https://repo.spring.io/snapshot</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>true</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </repository>&nbsp; &nbsp; &nbsp; &nbsp; <repository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>jfrog-snapshots</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>JFROG Snapshots</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>http://oss.jfrog.org/artifactory/oss-snapshot-local</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>true</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </repository>&nbsp; &nbsp; &nbsp; &nbsp; <repository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>spring-milestones</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>Spring Milestones</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>https://repo.spring.io/milestone</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>false</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </repository>&nbsp; &nbsp; </repositories>&nbsp; &nbsp; <pluginRepositories>&nbsp; &nbsp; &nbsp; &nbsp; <pluginRepository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>spring-snapshots</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>Spring Snapshots</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>https://repo.spring.io/snapshot</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>true</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </pluginRepository>&nbsp; &nbsp; &nbsp; &nbsp; <pluginRepository>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <id>spring-milestones</id>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <name>Spring Milestones</name>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <url>https://repo.spring.io/milestone</url>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <snapshots>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <enabled>false</enabled>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </snapshots>&nbsp; &nbsp; &nbsp; &nbsp; </pluginRepository>&nbsp; &nbsp; </pluginRepositories></project>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java