我能够像这样创建一个sessionfactory bean。
@Configuration
public class HibernateConfig {
private EntityManagerFactory emf;
@Bean
public HibernateJpaSessionFactoryBean sessionFactory() {
HibernateJpaSessionFactoryBean fact = new HibernateJpaSessionFactoryBean();
fact.setEntityManagerFactory(emf);
return fact;
}
@Autowired
public HibernateConfig(EntityManagerFactory emf) {
this.emf = emf;
}
}
但是使用@ImportResource
或@EntityScan
与jar的类路径或列出hbm.xml文件仍会产生not a managed type: class com.opensymphony.workflow.spi.hibernate.HibernateCurrentStep
我看到一些答案,你使用另一个类来生成sessionFactory bean。是否有一种简单的方法可以在实体管理器创建中包含映射文件?
我能够用
扫描hbm.xml文件 @Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.foo.bar.domain");
em.setMappingResources("classpath:x/HibernateCurrentStep.hbm.xml",
"classpath:x/HibernateHistoryStep.hbm.xml",
"classpath:x/HibernateWorkflowEntry.hbm.xml");
HibernateJpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
vendor.setShowSql(false);
em.setJpaVendorAdapter(vendor);
return em;
}
但是使用会话工厂的任何事务都会获得No CurrentSessionContext configured!
,而基本的spring repo方法仍然有效。
答案 0 :(得分:0)
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new
LocalContainerEntityManagerFactoryBean();
em.setDataSource(dataSource);
em.setPackagesToScan("com.foo.bar.domain");
em.setMappingResources("com/opensymphony/workflow/spi/hibernate3/HibernateCurrentStep.hbm.xml",
"com/opensymphony/workflow/spi/hibernate3/HibernateHistoryStep.hbm.xml",
"com/opensymphony/workflow/spi/hibernate3/HibernateWorkflowEntry.hbm.xml");
//these needed to be added to have all hibernate config done in one place.
em.getJpaPropertyMap().put(AvailableSettings.CURRENT_SESSION_CONTEXT_CLASS,
SpringSessionContext.class.getName());
em.getJpaPropertyMap().put(AvailableSettings.DIALECT,
PostgreSQL9Dialect.class.getName());
HibernateJpaVendorAdapter vendor = new HibernateJpaVendorAdapter();
vendor.setShowSql(false);
em.setJpaVendorAdapter(vendor);
return em;
}