当前位置: 首页 > 知识库问答 >
问题:

在spring boot中迁移到hibernate 5.2.6后,EntityManagerFactory不能为null

衡子安
2023-03-14

从Hibernate 5.0.11迁移到5.2.6后,我在Spring boot应用程序中使用Hibernate。最终的我得到EntityManagerFactory不能为空异常这是我的代码

我的Hibernate配置

@Configuration
@EnableTransactionManagement
public class DatabaseConfiguration {

    @Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }

    @Bean
    public HibernateTransactionManager transactionManager(@Autowired SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory);
        return transactionManager;
    }

}

我的仓库类

@Repository
public class GenericRepository{

    @Autowired
    private SessionFactory sessionFactory;

}

例外是这个

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: EntityManagerFactory must not be null
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:175)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:127)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1634)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:254)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1018)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:345)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340)
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.findEntityManagerFactory(EntityManagerFactoryUtils.java:143)
    at org.springframework.orm.jpa.EntityManagerFactoryAccessor.setBeanFactory(EntityManagerFactoryAccessor.java:137)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeAwareMethods(AbstractAutowireCapableBeanFactory.java:1647)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1615)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:208)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1138)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:585)
    ... 50 common frames omitted
Caused by: java.lang.IllegalStateException: **EntityManagerFactory must not be null**
    at org.springframework.util.Assert.state(Assert.java:392)
    at org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean.getObject(HibernateJpaSessionFactoryBean.java:46)
    at org.springframework.orm.jpa.vendor.HibernateJpaSessionFactoryBean.getObject(HibernateJpaSessionFactoryBean.java:41)
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:168)
    ... 71 common frames omitted

共有2个答案

鲁德佑
2023-03-14

只需从transactionManager bean配置中删除@Autowired,应该是这样的:

 @Bean
    public HibernateJpaSessionFactoryBean sessionFactory() {
        return new HibernateJpaSessionFactoryBean();
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        HibernateTransactionManager transactionManager = new HibernateTransactionManager();
        transactionManager.setSessionFactory(sessionFactory());
        return transactionManager;
    }
云光明
2023-03-14

我将create sessionfactory的配置更改为此

    @Inject
    public DataSource dataSource;


    @Inject
    public JpaProperties prop;


    @Bean("sessionFactory")
    @Primary
    public LocalSessionFactoryBean getSessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setPackagesToScan(new String[] { "org.roshan" });
        sessionFactory.setAnnotatedPackages(new String[] { "org.roshan" });
        sessionFactory.setHibernateProperties(getHibernateProperties());
        return sessionFactory;
    }


    private  Properties getHibernateProperties() {
        Properties p = new Properties();
        p.putAll(prop.getProperties());
        p.put(AvailableSettings.HBM2DDL_AUTO, prop.getHibernate().getDdlAuto());
        return p;
    }
 类似资料:
  • 最近,我尝试将firebase云函数从javascript迁移到typescript,并将函数拆分为多个文件。然而,我在尝试服务和部署时不断出错: 服务时的错误: 函数[FunctionName]:函数被忽略,因为fiRecovery模拟器不存在或未运行。函数[FunctionName]:函数被忽略,因为Firebase模拟器不存在或未运行。 部署时出错: 已尝试: Firestore/Fireb

  • 我买了新的MacBook Pro,我在其中安装了mysql,然后安装sequel Pro来查看表和数据库,但是当我命令时,这个错误指控错误图像 近30个小时来,我一直在忍受这个问题,有人能帮我看一下吗

  • 问题内容: 我正在尝试迁移到androidx。我在Android Studio中使用了迁移工具。当我执行此操作时,我在运行应用程序时得到以下堆栈跟踪。 例外是正确的。android.support.v4.content.FileProvider在我的应用中不存在。但是androidx.core.content.FileProvider包含在我的应用程序中。最大的问题是为什么它要加载旧版本的File

  • 我正在做一个Spring Boot项目,Spring Boot版本1.5.4.release.我们需要用户Hibernate5.2.10,所以我将hibernate.version设置为5.2.10。最终。有错误:java.lang.IllegalStateExctive:EntityManagerFactory必须为null。git上的代码https://github.com/shuanshua

  • 我有这个SpringBoot的代码。它加载mybatis和冬眠。我得到一个异常类型EntityManagerFactory不可用,但它是在这里定义。记录输出包含'xxx'全部打印。 在SpringBoot中,我遇到了以下异常:

  • 我在我支持的许多应用程序中实现了Flyway,它像梦一样工作。 但是,当我将应用程序部署到测试环境时,迁移就停止了。 经过一些调查,我发现当迁移被加载到jar文件中时,它们不是通过Flyway定位的,而是当它们没有被压缩时(比如当我在Eclipse中工作时,或者如果我将jar解压到类路径中),它会按预期工作。 由于应用程序的插件架构,我无法使用“默认”设置,因此我将Flyway对象设置为: 如果要