简体   繁体   中英

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration

I am using Spring Boot 2.0.0.RC1 (It include Spring Framework 5.0.3.RELEASE), Hibernate 5.2.12.Final, JPA 2.1 API 1.0.0.Final .

I have a class

package com.example;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManagerFactory;

@Configuration
public class BeanConfig {

    @Autowired
    EntityManagerFactory emf;

    @Bean
    public SessionFactory sessionFactory(@Qualifier("entityManagerFactory") EntityManagerFactory emf) {
        return emf.unwrap(SessionFactory.class);
    }

}

Then error

Error
***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method sessionFactory in com.example.BeanConfig required a bean of type 'javax.persistence.EntityManagerFactory' that could not be found.


Action:

Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration.


Process finished with exit code 1

How to fix this?

If you include this:

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

You won't have to autowire the Entity Manager or provide a Session Factory bean.

You would only need to provide JpaRepository interfaces like:

public interface ActorDao extends JpaRepository<Actor, Integer> {
}

where Actor is a JPA entity class and Integer is the ID / primary key and inject ActorDao in a service impl class.

The specific error you are having is caused by the @Qualifier annotation; Spring is looking for a Bean with the specific name you mentioned, instead of looking for any Bean of type EntityManagerFactory . Just remove the annotation.

However, once you fix that, and because you are also injecting the Bean in the method that constructs SessionFactory, Spring Boot will generate another error related to cyclic dependencies. To avoid that, just remove the parameter altogether from sessionFactory method, since you already injected EntityManagerFactory in your Config class.

This code will work :

@Bean
public SessionFactory sessionFactory() {
        return emf.unwrap(SessionFactory.class);
}

In BeanConfig , you should inject the JPA EntityManager via @PersistenceUnit , not @Autowired .

And remove the getSessionFactory since the Hibernate SessionFactory is already created internally and you can always unwrap the EntityManagerFactory .

Like this:

@Configuration
public class BeanConfig {

    @PersistenceUnit
    EntityManagerFactory emf;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM