我正在使用 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。
我有一个类
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);
}
}
然后报错
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
如何解决这个问题?
原文由 James Grey 发布,翻译遵循 CC BY-SA 4.0 许可协议
如果你包括这个:
您不必自动装配
Entity Manager
或提供Session Factory
bean。您只需要提供 JpaRepository 接口,例如:
where
Actor
is aJPA
entity class andInteger
is the ID / primary key and injectActorDao
in aservice
实施类。