【问题标题】:Spring Data JPA LocalEntityManagerFactoryBean getNativeEntityManagerFactory returns nullSpring Data JPA LocalEntityManagerFactoryBean getNativeEntityManagerFactory 返回 null
【发布时间】:2015-01-13 02:29:10
【问题描述】:

我正在尝试按照this post 上的 sds 指示将拦截器附加到我的 Hibernate JPA EntityManagerFactory,但使用 java config。

这是我的配置的相关部分:

@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager txm = new JpaTransactionManager();
    txm.setEntityManagerFactory(entityManagerFactory().getObject());
    return txm;
}

@SuppressWarnings("unchecked")
@Bean
public SynchronizedEntityChangeInterceptor synchronizedEntityChangeInterceptor() {
    SynchronizedEntityChangeInterceptor synchronizedEntityChangeInterceptor = new SynchronizedEntityChangeInterceptor();
    synchronizedEntityChangeInterceptor.registerEntityClasses(Device.class, Artist.class);
    return synchronizedEntityChangeInterceptor;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactory.setDataSource(dataSource());
    entityManagerFactory.setPersistenceUnitName("metamp-client");
    entityManagerFactory.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    Properties jpaProperties = new Properties();
    jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
    jpaProperties.put("hibernate.show_sql", "true");
    jpaProperties.put("hibernate.format_sql", "true");
    jpaProperties.put("hibernate.hbm2ddl.auto", "update");
    jpaProperties.put("hibernate.cache.use_second_level_cache", "false");
    entityManagerFactory.setJpaProperties(jpaProperties);
    return entityManagerFactory;
}

@Bean
public HibernateInterceptor hibernateInterceptor() {
    HibernateInterceptor hibernateInterceptor = new HibernateInterceptor();
    if(entityManagerFactory().getNativeEntityManagerFactory() == null)
            throw new NullPointerException("This shouldn't happen");
    hibernateInterceptor.setSessionFactory(
            ((HibernateEntityManagerFactory)entityManagerFactory()
                    .getNativeEntityManagerFactory()).unwrap(SessionFactory.class));
    hibernateInterceptor.setEntityInterceptor(synchronizedEntityChangeInterceptor());
    return hibernateInterceptor;
}

我的问题是“这不应该发生”总是会发生,所以 getNativeEntityManagerFactory() 返回 null 尽管 spring 文档说 otherwise

这是一个错误,还是我做错了什么?

【问题讨论】:

    标签: java spring hibernate spring-mvc jpa


    【解决方案1】:

    您不应该直接致电entityManagerFactory()。请尝试以下操作:

    @Bean
    public HibernateInterceptor hibernateInterceptor(
                LocalContainerEntityManagerFactoryBean factory) {
        HibernateInterceptor hibernateInterceptor = new HibernateInterceptor();
        if(factory.getNativeEntityManagerFactory() == null)
                throw new NullPointerException("This shouldn't happen");
        // rest of your code and use factory variable rather than entityManagerFactory()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-26
      • 2022-10-13
      • 2018-08-08
      • 2020-09-22
      • 2021-07-01
      相关资源
      最近更新 更多