2017-07-10 88 views
0

我能夠獲得像這樣創建的sessionfactory bean。Spring Boot + Hibernate包含依賴關係jar中的hbm.xml

@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與classpath中的jar或上市hbm.xml文件仍產生not a managed type: class com.opensymphony.workflow.spi.hibernate.HibernateCurrentStep

我看到一些答案,你使用不同的類來生成SessionFactory的豆。是否有一種簡單的方法來獲取包含在實體管理器創建中的映射文件?

我能夠掃描與

@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; 
} 

hbm.xml文件,但隨後使用會話工廠的交易變得No CurrentSessionContext configured!而基本春天回購方法仍然有效。

回答

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; 
} 
相關問題