我写了一个SpringBoot项目,当我在项目中使用
@Repository
public class xxDaoImpl implements xxDao {
@Autowired
private SessionFactory session;
我得到了如下错误
***************************
APPLICATION FAILED TO START
***************************
Description:
Field session in xx.dao.ParkingSpaceDaoImpl required a bean of type 'org.hibernate.SessionFactory' that could not be found.
Action:
Consider defining a bean of type 'org.hibernate.SessionFactory' in your configuration.
所以我找到了一个适合我的解决方案。正如您正在使用.xml配置文件,这样写可能是对的。但是对于那些正在使用application.properties文件的用户,您需要将此行添加到配置类或主应用程序类中。
@Bean
public SessionFactory sessionFactory(HibernateEntityManagerFactory hemf){
return hemf.getSessionFactory();
}
或者
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
return new HibernateJpaSessionFactoryBean();
}
完成后将下面的内容添加到application.properties配置文件中
spring.jpa.properties.hibernate.current_session_context_class=org.springframework.orm.hibernate4.SpringSessionContext
这样配置后,将成功启动项目,并成功创建sessionFactory,如果您也遇到了同样的问题,希望能帮助到你。
|
|
|
|