Я пытаюсь подключить перехватчик к моему 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.
Это ошибка, или я делаю что-то неправильно?