简体   繁体   English

将@Configuration 文件指向 hbm.xml

[英]Point @Configuration file to hbm.xml

I have a library that contains a few objects I would like to save in my database using hibernate.我有一个库,其中包含一些我想使用 hibernate 保存在我的数据库中的对象。

I ended up making hbm.xml files for every single object.我最终为每个对象制作了 hbm.xml 文件。 Now I have my AppConfig class现在我有了我的 AppConfig 类

@Configuration
@ComponentScan("some.company")
@EnableAutoConfiguration(exclude = HibernateJpaAutoConfiguration.class)
@EnableTransactionManagement
public class AppConfig
        extends WebMvcConfigurerAdapter
{
    @Bean(name = "dataSource")
    public DataSource dataSource()
    {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:mysql://localhost:3306/somedatabase");
        dataSource.setUsername("user");
        dataSource.setPassword("password");
        return dataSource;
    }

    @Bean(name = "sessionFactory")
    public LocalSessionFactoryBean sessionFactory()
    {
        LocalSessionFactoryBean localSession = new LocalSessionFactoryBean();
        localSession.setDataSource(dataSource());
        return localSession;
    }

    @Bean(name = "transactionManager")
    public HibernateTransactionManager transactionManager()
    {
        return new HibernateTransactionManager(sessionFactory().getObject());
    }
}

How do I point it to my newly created hbm.xml files?我如何将它指向我新创建的 hbm.xml 文件?

Or if that is not possible, then how would I go about creating hibernate mapping for the objects to which I have no write access to?或者,如果这是不可能的,那么我将如何为我没有写访问权限的对象创建休眠映射?
The only constraint is that I can not write my app config in xml, there is too much there that would not be movable to xml now.唯一的限制是我不能在 xml 中编写我的应用程序配置,那里有太多现在无法移动到 xml 的内容。

Pragmatically, I would go for:务实地,我会去:

@Bean(name = "sessionFactory")
public LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean localSession = new LocalSessionFactoryBean();
    localSession.setDataSource(dataSource());

    localSession.setMappingResources("my.hmb.xml", "files.hbm.xml");

    return localSession;
}

(when the hbm files reside in the root of class path - eg in src/main/resources ) (当 hbm 文件位于类路径的根目录时 - 例如在src/main/resources

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

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