2014-11-15 2 views
2

Я пытаюсь подключить перехватчик к моему Hibernate JPA EntityManagerFactory, следуя инструкциям sds на this post, но с конфигурацией java.Spring Data JPA LocalEntityManagerFactoryBean getNativeEntityManagerFactory возвращает null

Вот соответствующая часть моей конфигурации:

@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 хотя документация весной говорит otherwise.

Это ошибка, или я делаю что-то неправильно?

ответ

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() 
Смежные вопросы