简体   繁体   中英

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.

I ended up making hbm.xml files for every single object. Now I have my AppConfig class

@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?

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.

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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