为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕少森 2023-03-17 15:31:48
当我尝试使用 hibernate 和 MySql 运行我的 spring-boot 项目时,出现错误。我的应用程序.propertiesspring.datasource.url = jdbc:mysql://localhost:3306/spring-mysql?useSSL=falsespring.datasource.username = rootspring.datasource.password = spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialectspring.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
查看完整描述

6 回答

?
holdtom

TA贡献1805条经验 获得超10个赞

您使用了错误包中的 @Id 注释:

import org.springframework.data.annotation.Id;

它一定要是:

 import javax.persistence.Id;


查看完整回答
反对 回复 2023-03-17
?
茅侃侃

TA贡献1842条经验 获得超21个赞

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



查看完整回答
反对 回复 2023-03-17
?
料青山看我应如是

TA贡献1772条经验 获得超8个赞

前几天我收到错误消息,比如需要一个名为“entityManagerFactory”的 bean,但找不到。


经过大量谷歌搜索解决了这个问题。我为 JPA 设置了手动配置。


@Bean 

public LocalSessionFactoryBean sessionFactory() {

    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); 


    return sessionFactory;

}

但 JPA 默认按名称“entityManagerFactory”搜索 sessionFactory,因此将我的代码更改为:


@Bean(name="entityManagerFactory")

public LocalSessionFactoryBean sessionFactory() {

    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();


    return sessionFactory;

我之前的回答

更新 导入正确的类


import javax.persistence.Id;


您尝试在没有外键参考的情况下加入学科课程...根据该图像更改您的代码

//img1.sycdn.imooc.com//641417ce0001e04402210077.jpg

查看完整回答
反对 回复 2023-03-17
?
ibeautiful

TA贡献1993条经验 获得超5个赞

我遇到了同样的问题,我通过将版本添加到 pom.xml 中的 javax persistence api 来修复它,更改它:


    <dependency>

        <groupId>javax.persistence</groupId>

        <artifactId>javax.persistence-api</artifactId>

    </dependency>

为此:


    <dependency>

        <groupId>javax.persistence</groupId>

        <artifactId>javax.persistence-api</artifactId>

        <version>2.2</version>

    </dependency>


查看完整回答
反对 回复 2023-03-17
?
慕容708150

TA贡献1831条经验 获得超4个赞

添加@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")       /* ADDED HERE */

public class SpringBootMysqlApplication {


    public static void main(String[] args) {

        SpringApplication.run(SpringBootMysqlApplication.class, args);

    }


}


查看完整回答
反对 回复 2023-03-17
?
开满天机

TA贡献1786条经验 获得超12个赞

<?xml version="1.0" encoding="UTF-8"?>

<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>


    <parent>

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

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

        <version>2.3.1.RELEASE</version>

        <relativePath />

        <!-- lookup parent from repository -->

    </parent>


    <groupId>com.revature</groupId>

    <artifactId>restful-web-services</artifactId>

    <version>0.0.1-SNAPSHOT</version>

    <name>RestfulWebServices</name>

    <description>Restful Web Services project for Spring Boot</description>


    <properties>

        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

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

        <maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>

    </properties>


    <dependencies>


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->

        <dependency>

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

            <artifactId>spring-boot-starter-data-jpa</artifactId>

            <version>2.3.3.RELEASE</version>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->


        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-jdbc -->

        <dependency>

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

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

            <version>2.3.3.RELEASE</version>

        </dependency>


        <dependency>

            <groupId>com.h2database</groupId>

            <artifactId>h2</artifactId>

            <scope>runtime</scope>

        </dependency>


        <dependency>

            <groupId>org.postgresql</groupId>

            <artifactId>postgresql</artifactId>

            <version>42.2.5</version>

        </dependency>


        <dependency>

            <groupId>org.slf4j</groupId>

            <artifactId>slf4j-api</artifactId>

            <version>1.7.25</version>

        </dependency>



        <dependency>

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

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

        </dependency>

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

            </dependency> -->

        <!-- START AND STOP THE APPLICATION AFTER MAKING THE CHANGE! -->

        <dependency>

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

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

        </dependency>


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

            </dependency> -->


        <!-- provides information concerning the health metrics etc. of applicaiton -->

        <dependency>

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

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

        </dependency>


        <!-- provides view of data and consumes info from actuator -->

        <dependency>

            <groupId>org.springframework.data</groupId>

            <artifactId>spring-data-rest-hal-browser</artifactId>

        </dependency>



        <dependency>

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

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

        </dependency>


        <dependency>

            <groupId>com.fasterxml.jackson.dataformat</groupId>

            <artifactId>jackson-dataformat-xml</artifactId>

        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.maven.plugin-tools/maven-plugin-annotations -->


        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-boot-starter</artifactId>

            <version>3.0.0</version>

        </dependency>


        <dependency>

            <groupId>io.springfox</groupId>

            <artifactId>springfox-boot-starter</artifactId>

            <version>3.0.0</version>

        </dependency>

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

            </dependency> -->

        <dependency>

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

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

            <scope>runtime</scope>

        </dependency>


        <dependency>

            <groupId>javax.xml.bind</groupId>

            <artifactId>jaxb-api</artifactId>

            <version>2.3.0</version>

        </dependency>




        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.9</version>

        </dependency>



        <dependency>

            <groupId>org.mockito</groupId>

            <artifactId>mockito-core</artifactId>

            <version>1.9.5</version>

            <scope>test</scope>

        </dependency>


        <dependency>

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

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

            <scope>test</scope>

        </dependency>



    </dependencies>


    <build>

        <plugins>

            <plugin>

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

                <artifactId>spring-boot-maven-plugin</artifactId>

            </plugin>

        </plugins>

    </build>


    <repositories>

        <repository>

            <id>spring-snapshots</id>

            <name>Spring Snapshots</name>

            <url>https://repo.spring.io/snapshot</url>

            <snapshots>

                <enabled>true</enabled>

            </snapshots>

        </repository>

        <repository>

            <id>jfrog-snapshots</id>

            <name>JFROG Snapshots</name>

            <url>http://oss.jfrog.org/artifactory/oss-snapshot-local</url>

            <snapshots>

                <enabled>true</enabled>

            </snapshots>

        </repository>

        <repository>

            <id>spring-milestones</id>

            <name>Spring Milestones</name>

            <url>https://repo.spring.io/milestone</url>

            <snapshots>

                <enabled>false</enabled>

            </snapshots>

        </repository>

    </repositories>


    <pluginRepositories>

        <pluginRepository>

            <id>spring-snapshots</id>

            <name>Spring Snapshots</name>

            <url>https://repo.spring.io/snapshot</url>

            <snapshots>

                <enabled>true</enabled>

            </snapshots>

        </pluginRepository>

        <pluginRepository>

            <id>spring-milestones</id>

            <name>Spring Milestones</name>

            <url>https://repo.spring.io/milestone</url>

            <snapshots>

                <enabled>false</enabled>

            </snapshots>

        </pluginRepository>

    </pluginRepositories>


</project>


查看完整回答
反对 回复 2023-03-17
  • 6 回答
  • 0 关注
  • 135 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信