我有一个库,其中包含一些我希望使用hibernate保存在数据库中的对象。
我最终为每个对象制作了hbm.xml文件。现在我有了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());
}
}
如何将其指向我新创建的hbm.xml文件?
或者,如果那是不可能的,那么我将如何为我无法访问的对象创建hibernate映射?
唯一的限制是我无法在xml中编写我的app配置,有太多的东西现在无法移动到xml。
答案 0 :(得分:1)
务实,我会选择:
@Bean(name = "sessionFactory")
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean localSession = new LocalSessionFactoryBean();
localSession.setDataSource(dataSource());
localSession.setMappingResources("my.hmb.xml", "files.hbm.xml");
return localSession;
}
(当hbm文件位于类路径的根目录中时 - 例如在src/main/resources
中)