Spring引导 - 将Hibernate映射文件添加到实体管理器 [英] Spring-boot - add hibernate mapping file to entity manager

查看:212
本文介绍了Spring引导 - 将Hibernate映射文件添加到实体管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我将旧版应用程序迁移到Spring引导,我们提出了一个解决方案,它包含一个


  ... 
@Autowired
私有DataSource数据源;

@Bean
public LocalSessionFactoryBean sessionFactory(){
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);

// ...

sessionFactoryBean.setMappingResources(META-INF / named-queries.hbm.xml);

return sessionFactoryBean;
}

但是我终于有了一个entityManager bean和一个sessionFactory bean在我的应用程序!

根据你的解决方案是一个很好的解决方案吗?
有没有办法在不使用sessionFactory bean的情况下将hibernate映射文件(named-query.hbm.xml)添加到entityManager中?

谢谢为您提前建议

**编辑**
来自JB Nizet的建议,也想出了另一个解决方案




@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(){
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean );

entityManagerFactory.setDataSource(dataSource);

// ...

entityManagerFactory.setMappingResources(META-INF / named-queries.hbm.xml);

返回entityManagerFactory;
}

并且在我的DAO / Service中,我仍然可以获得hibernate会话:



  private Session getSession ){
// return this.sessionFactory.getCurrentSession();
返回this.entityManager.unwrap(Session.class);
}

但如果有人发现我们可以做同样的事情,用spring-boot自动配置属性,很受欢迎!

解决方案

  @Autowired 
私有ResourceLoader rl;


$ be $ b $ public LocalSessionFactoryBean sessionFactory()抛出IOException {
LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
sessionFactoryBean.setMappingLocations(loadResources());
}

public Resource [] loadResources(){
Resource [] resources = null;
尝试{
resources = ResourcePatternUtils.getResourcePatternResolver(rl)
.getResources(classpath:/ hibernate / *。hbm.xml);
} catch(IOException e){
// TODO自动生成的catch块
e.printStackTrace();
}
返回资源;
}


I'm migrating legacy app to Spring-boot and have to integrate an hibernate named query mapping file (previously configured in persitence.xml file).

I've come out with a solution with an

...
@Autowired
private DataSource dataSource;

@Bean
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource);

    //...

    sessionFactoryBean.setMappingResources("META-INF/named-queries.hbm.xml");

    return sessionFactoryBean;
}   

But i'm ending having an entityManager bean and a sessionFactory bean in my application!

is it a good solution according to you? Is there a way to add somehow the hibernate mapping file (named-query.hbm.xml) to the entityManager without using the sessionFactory bean?

Thanks in advance for you suggestions

** EDIT ** fro JB Nizet's suggestion, also come up with another solution

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();

    entityManagerFactory.setDataSource(dataSource);

    // ...

    entityManagerFactory.setMappingResources("META-INF/named-queries.hbm.xml");

    return entityManagerFactory;
}

and in my DAO/Service, i can still get hibernate session with:

private Session getSession() {
        //return this.sessionFactory.getCurrentSession();
        return this.entityManager.unwrap(Session.class);
    }

But if someone nows if we can do the same thing with spring-boot auto-config with properties, it's welcomed!

解决方案

@Autowired
private ResourceLoader rl;


@Bean
public LocalSessionFactoryBean sessionFactory() throws IOException {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setMappingLocations(loadResources());
}

public Resource[] loadResources() {
    Resource[] resources = null;
    try {
        resources = ResourcePatternUtils.getResourcePatternResolver(rl)
            .getResources("classpath:/hibernate/*.hbm.xml");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return resources;
}

这篇关于Spring引导 - 将Hibernate映射文件添加到实体管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆