考虑在你的配置中定义一个“javax.persistence.EntityManager”类型的bean

Posted

技术标签:

【中文标题】考虑在你的配置中定义一个“javax.persistence.EntityManager”类型的bean【英文标题】:Consider defining a bean of type 'javax.persistence.EntityManager' in your configuration 【发布时间】:2020-08-01 16:59:20 【问题描述】:

我是春天的初学者。所以现在我开始学习spring boot并构建这个简单的项目,但是当我运行它时,我得到了这个错误“ sagala.rest.boot.remade.dao.EmployeeDaoImpl 中的字段 entityManager 需要一个找不到的 'javax.persistence.EntityManager' 类型的 bean。”。

这是我的控制器类

package sagala.rest.boot.remade.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sagala.rest.boot.remade.entity.Employee;
import sagala.rest.boot.remade.service.EmployeeService;


@RestController
@RequestMapping("/api")
public class MainController 

@Autowired
private EmployeeService employeeService;

@GetMapping("/employees")
public List<Employee> findAll() 
    return employeeService.findAll();



这里是服务类

package sagala.rest.boot.remade.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sagala.rest.boot.remade.dao.EmployeeDao;
import sagala.rest.boot.remade.entity.Employee;

@Service
public class EmployeeServiceImpl implements EmployeeService 

@Autowired
private EmployeeDao employeeDao;

@Override
@Transactional
public List<Employee> findAll() 

    return employeeDao.findAll();



这是 DAO 类

package sagala.rest.boot.remade.dao;

import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sagala.rest.boot.remade.entity.Employee;

@Repository
public class EmployeeDaoImpl implements EmployeeDao 

@Autowired
private EntityManager entityManager;

@Override
public List<Employee> findAll() 
    Session session =entityManager.unwrap(Session.class);
    Query<Employee> query =session.createQuery("from Employee", Employee.class);
    List<Employee> employees =query.getResultList();
    return employees;



这里是实体类

package sagala.rest.boot.remade.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="employee")
public class Employee 

@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="first_name")
private String firstName;

@Column(name="last_name")
private String lastName;

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

public Employee() 



public Employee(String firstName, String lastName, String email) 
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;


public int getId() 
    return id;


public void setId(int id) 
    this.id = id;


public String getFirstName() 
    return firstName;


public void setFirstName(String firstName) 
    this.firstName = firstName;


public String getLastName() 
    return lastName;


public void setLastName(String lastName) 
    this.lastName = lastName;


public String getEmail() 
    return email;


public void setEmail(String email) 
    this.email = email;



这是 POM 文件

<?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.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>sagala</groupId>
<artifactId>rest.boot.remade</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rest.boot.remade</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
    <maven-jar-plugin.version>3.1.1</maven-jar-plugin.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>
        <optional>true</optional>
    </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>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </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/employee_directory?useSSL=false&serverTimezone=UTC spring.datasource.username=spring******* spring.datasource.password=spring*******

如果有人可以帮助我。谢谢

【问题讨论】:

您应该使用@PersistenceContext 而不是@Autowired。请通过这个问题***.com/q/31335211/7458887。 已经试过了,但还是不行。 【参考方案1】:

我将 Spring Boot 版本从 2.2.6 更改为 2.1.13,一切正常。似乎 javax.persistence.EntityManager 在较新的 Spring Boot 版本中已损坏。即使多次删除 repo,当我尝试将其扩展为另一种替代 dao 实现时,我仍然找不到 JpaRepository 接口。

【讨论】:

以上是关于考虑在你的配置中定义一个“javax.persistence.EntityManager”类型的bean的主要内容,如果未能解决你的问题,请参考以下文章

考虑在你的配置中定义一个“javax.persistence.EntityManager”类型的bean

如何在你的在线IDE中配置zsh作为默认shell

考虑在你的下一个Web项目中使用VueJS吧!

在你的网站服务器配置https

基于 Confluence 6 数据中心在你的 Atlassian 应用中配置 SAML 授权

基于 Confluence 6 数据中心在你的 Atlassian 应用中配置 SAML 授权