当前位置: 首页 > 知识库问答 >
问题:

Controller需要名为“Entity ManagerFactory”得bean,但找不到该bean.-Spring靴

皇甫文乐
2023-03-14

当我启动我的springboot应用程序时,我得到一个错误

field materialRequestRepo in com.sql.csse.ControllerManager.MaterialRequestController required a bean named 'entityManagerFactory' that could not be found.
<dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-core</artifactId>
 <version>4.1.4.Final</version>
</dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>5.2.3.Final</version>
</dependency>
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 org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]

对于这个问题,正如您在这里看到的,他们要求添加与我之前删除的相同的依赖项。

这里可能发生了什么问题?

pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sql.test</groupId>
  <artifactId>SpringBoot-sqlTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringBoot-sqlTest</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.5.RELEASE</version>
    <relativePath/>
    <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

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

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

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

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.6</version>
    </dependency>

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

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.2</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-core</artifactId>
      <version>4.3.11.Final</version>
    </dependency>
    <dependency>
      <groupId>org.hibernate</groupId>
      <artifactId>hibernate-entitymanager</artifactId>
      <version>5.2.3.Final</version>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>


</project>
package com.sql.csse.ControllerManager;


import com.google.gson.Gson;
import com.sql.csse.EntityManager.MaterialRequest;
import com.sql.csse.EntityManager.Order;
import com.sql.csse.EntityManager.Supplier;
import com.sql.csse.RepositoryManager.MaterialRequestRepo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/Requests")
public class MaterialRequestController {

    @Autowired
    MaterialRequestRepo materialRequestRepo;

    MaterialRequest materialRequest;
    List<MaterialRequest> materialList;

    @RequestMapping(method = RequestMethod.POST , value = "/save" , produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MaterialRequest> MaterialRequests(@RequestBody  String mr){

        Gson gson = new Gson();
        materialRequest =  gson.fromJson(mr, MaterialRequest.class);
        materialRequestRepo.save(materialRequest);
        return materialRequestRepo.findAll();
    }

    @RequestMapping(method = RequestMethod.GET , value = "/getall" , produces = MediaType.APPLICATION_JSON_VALUE)
    public List<MaterialRequest> getAll(){
        return materialRequestRepo.findAll();
    }

    @RequestMapping(method = RequestMethod.GET , value = "/getallPending" , produces = MediaType.APPLICATION_JSON_VALUE)
    public ArrayList<MaterialRequest> getPendingRequests(){

        return materialRequestRepo.findpendingRequests();


    }

}

MaterialRequest(实体)

package com.sql.csse.EntityManager;


import javax.persistence.*;

@Entity

@Table(name ="material_requests")
public class MaterialRequest {

    @Id
    @Column(name = "RID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int RID;


    @Column(name = "MID")
    private int MID;

    @Column(name = "material_name")
    private String material_name;

    @Column(name = "material_quantity")
    private double material_quantity;

    @Column(name = "requested_date")
    private String requested_date;

    @Column(name = "order_date")
    private String order_date;

    @Column(name = "satatus")
    private String status;


    public MaterialRequest(int MID, String material_name, double material_quantity, String requested_date, String order_date, String status) {
        this.MID = MID;
        this.material_name = material_name;
        this.material_quantity = material_quantity;
        this.requested_date = requested_date;
        this.order_date = order_date;
        this.status = status;

    }

    public MaterialRequest() {
    }

    public int getRID() {
        return RID;
    }

    public void setRID(int RID) {
        this.RID = RID;
    }

    public int getMID() {
        return MID;
    }

    public void setMID(int MID) {
        this.MID = MID;
    }

    public String getMaterial_name() {
        return material_name;
    }

    public void setMaterial_name(String material_name) {
        this.material_name = material_name;
    }

    public double getMaterial_quantity() {
        return material_quantity;
    }

    public void setMaterial_quantity(double material_quantity) {
        this.material_quantity = material_quantity;
    }

    public String getRequested_date() {
        return requested_date;
    }

    public void setRequested_date(String requested_date) {
        this.requested_date = requested_date;
    }

    public String getOrder_date() {
        return order_date;
    }

    public void setOrder_date(String order_date) {
        this.order_date = order_date;
    }

    public String getStatus() { return status; }

    public void setStatus(String status) { this.status = status; }
}

共有1个答案

桑成荫
2023-03-14

我复制了您的代码,使用这个pom.xml可以很好地工作:

 <project>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <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-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

这是我的应用程序。属性

spring.datasource.url=jdbc:mysql://localhost:3306/dto
spring.datasource.username=root
spring.datasource.password=root

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.jpa.hibernate.ddl-auto=update
 类似资料: