Java 在sessionFactory初始化之前更改实体架构名称
在从Hibernate3版本迁移到4版本的过程中,我遇到了一个问题。 我在项目中使用spring和hibernate,在应用程序启动期间,有时我想更改实体类的模式。对于3版本的hibernate和spring,我通过重写LocalSessionFactorBean类中的后处理配置方法来实现这一点,如下所示:Java 在sessionFactory初始化之前更改实体架构名称,java,spring,hibernate,Java,Spring,Hibernate,在从Hibernate3版本迁移到4版本的过程中,我遇到了一个问题。 我在项目中使用spring和hibernate,在应用程序启动期间,有时我想更改实体类的模式。对于3版本的hibernate和spring,我通过重写LocalSessionFactorBean类中的后处理配置方法来实现这一点,如下所示: @SuppressWarnings("unchecked") @Override protected void postProcessAnnotationConfigurat
@SuppressWarnings("unchecked")
@Override
protected void postProcessAnnotationConfiguration(AnnotationConfiguration config)
{
Iterator<Table> it = config.getTableMappings();
while (it.hasNext())
{
Table table = it.next();
table.setSchema(schemaConfigurator.getSchemaName(table.getSchema()));
}
}
@Override
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
sfb.buildMappings();
// For my task we need this
Iterator<Table> iterator = getConfiguration().getTableMappings();
while (iterator.hasNext()){
Table table = iterator.next();
if(table.getSchema() != null && !table.getSchema().isEmpty()){
table.setSchema(schemaConfigurator.getSchemaName(table.getSchema()));
}
}
return super.buildSessionFactory(sfb);
}
@SuppressWarnings(“未选中”)
@凌驾
受保护的无效后处理AnnotationConfiguration(AnnotationConfiguration配置)
{
迭代器it=config.getTableMappings();
while(it.hasNext())
{
Table=it.next();
table.setSchema(schemaConfigurator.getSchemaName(table.getSchema());
}
}
这件工作对我来说太完美了。但在hibernate4.LocalSessionFactoryBean类中,所有后期处理方法都被删除。有些人建议使用
ServiceRegistryBuilder
类,但我想在会话工厂中使用SpringXML配置,而对于ServiceRegistryBuilder
类,我不知道如何执行此操作。因此,可能有人会对我的问题提出任何解决方案。查看源代码帮助以找到解决方案LocalSessionFactoryBean
类的方法名为buildSessionFactory
(newSessionFactory
在以前的版本中)。对于以前版本的Hibernate
(3版本),在此方法调用之前已处理的某些操作。你可以在官方文件中看到它们
// Tell Hibernate to eagerly compile the mappings that we registered,
// for availability of the mapping information in further processing.
postProcessMappings(config);
config.buildMappings();
据我所知(可能是我错了),此buildMapping
方法解析指定为映射类或放置在packagesToScan
中的所有类,并创建所有此类的表表示。然后调用后处理配置
方法
对于Hibernate
4版本,我们没有这样的后处理方法。但是我们可以像这样重写buildSessionFactory
方法:
@SuppressWarnings("unchecked")
@Override
protected void postProcessAnnotationConfiguration(AnnotationConfiguration config)
{
Iterator<Table> it = config.getTableMappings();
while (it.hasNext())
{
Table table = it.next();
table.setSchema(schemaConfigurator.getSchemaName(table.getSchema()));
}
}
@Override
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
sfb.buildMappings();
// For my task we need this
Iterator<Table> iterator = getConfiguration().getTableMappings();
while (iterator.hasNext()){
Table table = iterator.next();
if(table.getSchema() != null && !table.getSchema().isEmpty()){
table.setSchema(schemaConfigurator.getSchemaName(table.getSchema()));
}
}
return super.buildSessionFactory(sfb);
}
@覆盖
受保护的SessionFactory构建SessionFactory(LocalSessionFactoryBuilder sfb){
buildMappings();
//为了完成我的任务,我们需要这个
迭代器迭代器=getConfiguration().getTableMappings();
while(iterator.hasNext()){
Table=iterator.next();
if(table.getSchema()!=null&&!table.getSchema().isEmpty()){
table.setSchema(schemaConfigurator.getSchemaName(table.getSchema());
}
}
返回super.buildSessionFactory(sfb);
}