简体   繁体   English

一个组件需要一个无法找到的类型为“javax.persistence.EntityManagerFactory”的 bean

[英]A component required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found

I just tried springboot through the tutorial but when I run it shows an error.我刚刚通过教程尝试了 springboot,但是当我运行它时显示错误。 i dont understand about this error.我不明白这个错误。

"Description: “描述:

Parameter 0 of method setEmf in com.mii.tugas.Dao.MahasiswaDao required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found. com.mii.tugas.Dao.MahasiswaDao 中方法 setEmf 的参数 0 需要无法找到类型为“javax.persistence.EntityManagerFactory”的 bean。

Action:行动:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration."考虑在您的配置中定义一个类型为 'javax.persistence.EntityManagerFactory' 的 bean。”

this my com.mii.tugas.Dao.MahasiswaDao :这是我的 com.mii.tugas.Dao.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();
    }
}

and this my MahasiswaServices :这是我的 MahasiswaServices :

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

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

My Pom.xml :我的 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>

You are mixing EclipseLink and JPA.您正在混合使用 EclipseLink 和 JPA。 Clean up the pom.xml first:首先清理 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>

Then you don't need to inject the EntityMangerFactory you can directly inject the EntityManager那么就不需要注入EntityMangerFactory直接注入EntityManager

But you even don't the EntityManger !但你甚至没有EntityManger

Please read the documentation of Spring Data JPA: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference请阅读 Spring Data JPA 的文档: https : //docs.spring.io/spring-data/jpa/docs/current/reference/html/#reference

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.example.daoImpl.FileDaoImpl中的field entityManagerFactory需要找不到类型为&#39;javax.persistence.EntityManagerFactory&#39;的bean - Field entityManagerFactory in com.example.daoImpl.FileDaoImpl required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found MultitenantConfiguration:未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean:预期的单个匹配Bean,但找到2 - MultitenantConfiguration: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean ::期望单个匹配的Bean,但找到2 - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined:: expected single matching bean but found 2 错误:没有定义类型为[javax.persistence.EntityManagerFactory]的合格bean:期望的单个匹配bean,但找到2 - error No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 没有定义类型为[javax.persistence.EntityManagerFactory]的合格Bean:期望单个匹配的Bean,但找到2 - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2 在JPA逆向工程师之后-没有定义[javax.persistence.EntityManagerFactory]类型的唯一bean:期望单个bean,但找到0 - After JPA Reverse Engineer - No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 0 Spring JPA(Hibernate)没有类型的限定bean:javax.persistence.EntityManagerFactory - Spring JPA (Hibernate) No qualifying bean of type: javax.persistence.EntityManagerFactory 使用Spring 4未定义类型为[javax.persistence.EntityManagerFactory]的合格Bean - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined Using Spring 4 NoSuchBeanDefinitionException:没有可用的“javax.persistence.EntityManagerFactory”类型的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type 'javax.persistence.EntityManagerFactory' available 没有定义类型为[javax.persistence.EntityManagerFactory]的合格bean - No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM