【发布时间】: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