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

组件需要无法找到的javax.persistence.EntityManagerFactory类型的bean

郜驰
2023-03-14

我只是试图通过本教程springboot,但当我运行它显示一个错误。我不明白这个错误。

”描述:

com.mii.tugas.中setEmf方法的参数0。Mahasiswao需要一个无法找到的'javax.persistence.EntityManagerFactory'类型的bean。

措施:

考虑定义“javax”类型的bean。坚持不懈配置中的EntityManagerFactory。"

这是我的com。信息产业部。图加斯。道。MahasiswaDao:

import com.mii.tugas.model.Mahasiswa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mii.tugas.interfaces.MahasiswaServices;

@Service
public class MahasiswaDao implements MahasiswaServices {

    private EntityManagerFactory emf;

    @Autowired
    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    @Override
    public List<Mahasiswa> listMahasiswa() {
        EntityManager em = emf.createEntityManager();
        return em.createQuery("from Data_Mahasiswa", Mahasiswa.class).getResultList();
    }
}

这是我的MahasiswaServices:

import com.mii.tugas.model.Mahasiswa;
import java.util.List;

public interface MahasiswaServices {
    List <Mahasiswa> listMahasiswa();
}

我的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 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.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mii</groupId>
    <artifactId>tugas</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tugas</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</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>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>eclipselink</artifactId>
            <version>2.5.2</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.persistence</groupId>
            <artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
            <version>2.5.2</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
        </dependency>
    </dependencies>

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

</project>

共有1个答案

萧嘉禧
2023-03-14

你正在混合EclipseLink和JPA。清理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 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.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mii</groupId>
    <artifactId>tugas</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>tugas</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.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>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
           <groupId>org.hibernate</groupId>
           <artifactId>hibernate-jpamodelgen</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>

然后你不需要注入EntityMangerFactory你可以直接注入EntityManager

但是你甚至没有EntityManger

请阅读Spring Data的留档JPA:https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

 类似资料:
  • 我有一个java项目,它将Spring Boot与JPA结合使用,并将Hibernate用于数据库。我正在尝试建立一个访问数据库的微服务。(我不熟悉微服务和Spring Boot)。 以下是主要课程: IGmCircularsDAO. class: GMCircularsDAOImpl。类别: ParentDAO。班 循环服务。班 当我运行这段代码时,我遇到了以下错误,我已经陷入其中一段时间了。

  • 使用mvn spring boot启动时:run或甚至使用gradle返回该问题。 这里是主要的类,在我看来所有的需求都可以,我使用的是org.springframework.boot版本1.5.7 以及: 使用Maven或Gradle会返回相同的问题。所有注释和包名称似乎都符合要求。

  • 问题内容: 每当启动应用程序spring启动时,我都会收到以下错误。 申请开始失败 描述: com.base.model.AbstractDao中的现场会话需要找不到“ org.hibernate.SessionFactory”类型的Bean。 行动: 考虑在配置中定义类型为“ org.hibernate.SessionFactory”的bean。 我添加了我的应用程序的实现: POM.xml 应

  • 我已经提到了为什么在Spring Boot期间找不到bean?和“字段需要找不到类型的bean。”使用mongodb的spring restful API出错 CustomerService只是一个接口,充当CustomerController和CustomerService实施之间的中间人,它实际上在CustomerRepository的帮助下对数据库执行操作。 我正在尝试从MYSQL数据库中检

  • 我是一名spring boot学习者,所以我一直在尝试创建一些基本的spring boot应用程序。我试图运行开发的应用程序时出错。 我的错误是[[https://i.stack.imgur.com/oyQDi.png][1]][1] java: ItemDetails.java:[软件包名称:io.ajithan.springbootstarter.model] ItemResponse.jav