Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在sessionFactory初始化之前更改实体架构名称_Java_Spring_Hibernate - Fatal编程技术网

Java 在sessionFactory初始化之前更改实体架构名称

Java 在sessionFactory初始化之前更改实体架构名称,java,spring,hibernate,Java,Spring,Hibernate,在从Hibernate3版本迁移到4版本的过程中,我遇到了一个问题。 我在项目中使用spring和hibernate,在应用程序启动期间,有时我想更改实体类的模式。对于3版本的hibernate和spring,我通过重写LocalSessionFactorBean类中的后处理配置方法来实现这一点,如下所示: @SuppressWarnings("unchecked") @Override protected void postProcessAnnotationConfigurat

在从Hibernate3版本迁移到4版本的过程中,我遇到了一个问题。 我在项目中使用spring和hibernate,在应用程序启动期间,有时我想更改实体类的模式。对于3版本的hibernate和spring,我通过重写LocalSessionFactorBean类中的后处理配置方法来实现这一点,如下所示:

@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);
}