java 考虑在您的配置中定义一个“javax.persistence.EntityManagerFactory”类型的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48888603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consider defining a bean of type 'javax.persistence.EntityManagerFactory' in your configuration
提问by Do Nhu Vy
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 .
我正在使用 Spring Boot 2.0.0.RC1(它包括 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?
如何解决这个问题?
回答by ootero
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 Manageror provide a Session Factorybean.
您不必自动装配Entity Manager或提供Session Factorybean。
You would only need to provide JpaRepository interfaces like:
您只需要提供 JpaRepository 接口,例如:
public interface ActorDao extends JpaRepository<Actor, Integer> {
}
where Actoris a JPAentity class and Integeris the ID / primary key and inject ActorDaoin a serviceimpl class.
其中Actor是一个JPA实体类和Integer为ID /主键和注入ActorDao在serviceimpl类。
回答by HL'REB
The specific error you are having is caused by the @Qualifierannotation; 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.
您遇到的特定错误是由@Qualifier注释引起的;Spring 正在寻找具有您提到的特定名称的 Bean,而不是寻找任何类型为 的 Bean EntityManagerFactory。只需删除注释。
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 sessionFactorymethod, since you already injected EntityManagerFactoryin your Config class.
但是,一旦您修复了该问题,并且由于您还在构造 SessionFactory 的方法中注入了 Bean,Spring Boot 将生成另一个与循环依赖相关的错误。为避免这种情况,只需从sessionFactory方法中完全删除参数,因为您已经注入EntityManagerFactory了 Config 类。
This code will work :
此代码将工作:
@Bean
public SessionFactory sessionFactory() {
return emf.unwrap(SessionFactory.class);
}
回答by Vlad Mihalcea
In BeanConfig, you should inject the JPA EntityManagervia @PersistenceUnit, not @Autowired.
在 中BeanConfig,您应该EntityManager通过@PersistenceUnit而不是注入 JPA @Autowired。
And remove the getSessionFactorysince the Hibernate SessionFactory is already created internally and you can always unwrap the EntityManagerFactory.
并删除 ,getSessionFactory因为 Hibernate SessionFactory 已经在内部创建,您可以随时解开EntityManagerFactory.
Like this:
像这样:
@Configuration
public class BeanConfig {
@PersistenceUnit
EntityManagerFactory emf;
}

