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 Manager
or provide a Session Factory
bean.
您不必自动装配Entity Manager
或提供Session Factory
bean。
You would only need to provide JpaRepository interfaces like:
您只需要提供 JpaRepository 接口,例如:
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.
其中Actor
是一个JPA
实体类和Integer
为ID /主键和注入ActorDao
在service
impl类。
回答by HL'REB
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.
您遇到的特定错误是由@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 sessionFactory
method, since you already injected EntityManagerFactory
in 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 EntityManager
via @PersistenceUnit
, not @Autowired
.
在 中BeanConfig
,您应该EntityManager
通过@PersistenceUnit
而不是注入 JPA @Autowired
。
And remove the getSessionFactory
since 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;
}