简体   繁体   English

Spring Boot + Hibernate从依赖项jar包含hbm.xml

[英]Spring Boot + Hibernate include hbm.xml from dependency jar

I'm able to get a sessionfactory bean created like so. 我能够像这样创建一个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;
    }

}

But using @ImportResource or @EntityScan with the classpath to the jar or listing the hbm.xml files still yields not a managed type: class com.opensymphony.workflow.spi.hibernate.HibernateCurrentStep 但是,将@ImportResource@EntityScan与jar的类路径一起使用或列出hbm.xml文件仍然not a managed type: class com.opensymphony.workflow.spi.hibernate.HibernateCurrentStep

I see some answers where you use a different class to generate the sessionFactory bean. 在使用其他类生成sessionFactory bean的地方,我看到了一些答案。 Is there an easy way to get the mapping files included in the Entity manager creation? 是否有一种简单的方法来获取包含在实体管理器创建中的映射文件?

I was able to scan the hbm.xml files with 我可以用扫描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;
}

but then any transaction that uses the session factory gets No CurrentSessionContext configured! 但是使用会话工厂的任何事务都No CurrentSessionContext configured! while basic spring repo methods still work. 虽然基本的春季回购方法仍然有效。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM