在sessionFactory初始化之前更改实体模式名称 [英] changing entity schema name before sessionFactory inicialization

查看:150
本文介绍了在sessionFactory初始化之前更改实体模式名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从hibernate 3版本迁移到4版本期间,我遇到了问题。
我在我的项目中使用spring和hibernate,并且在我的应用程序启动过程中,有时我想更改我的实体类的模式。使用3版本的hibernate和spring,我通过在LocalSessionFactortBean类中重写postProcessConfiguration方法来做到这一点:

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

这对我来说非常适合。但是在hibernate4.LocalSessionFactoryBean类中所有后期处理方法都被删除了。有人建议使用 ServiceRegistryBuilder 类,但我想为我的会话工厂使用spring xml配置,并使用 ServiceRegistryBuilder 类我不知道如何执行此操作。所以可能会有人提出任何解决我的问题。

在以前的版本中, LocalSessionFactoryBean 类的方法称为 buildSessionFactory newSessionFactory )。使用之前版本的 Hibernate (3版本)在此方法调用之前处理的一些操作。您可以在官方文档中看到它们

  //告诉Hibernate急切地编译我们注册的映射
//用于进一步处理中的映射信息的可用性。
postProcessMappings(config);
config.buildMappings();

据我所知(可能是我错了) buildMapping 方法解析所有指定为映射类的类或放置在 packagesToScan 中的类,并创建所有这些类的表格表示。在这个名为 postProcessConfiguration 方法之后。

使用 Hibernate 4版本我们没有这样的postProcess方法。但我们可以覆盖 buildSessionFactory 方法,如下所示:

  @Override 
protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb){
sfb.buildMappings();
//对于我的任务,我们需要这个
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);
}


During migration from hibernate 3 version to 4 version I faced with problem. I use spring and hibernate in my project and during start up of my application sometimes I want to change schema of my entity classes. With 3 version hibernate and spring I make this by overriding postProcessConfiguration method in LocalSessionFactortBean class like this:

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

this work perfect for me. But in hibernate4.LocalSessionFactoryBean class all post process methods were deleted. Some people suggest to use ServiceRegistryBuilder class, but I want to use spring xml configuration for my session factory and with ServiceRegistryBuilder class I don't know how to perform this. So may be someone suggest any solution to my problem.

解决方案

Looking at source code help to find solution. LocalSessionFactoryBean class has method called buildSessionFactory(newSessionFactory in previous version). With previous version of Hibernate(3 version) some operations where processed before this method call. You can see them in official docs

        // Tell Hibernate to eagerly compile the mappings that we registered,
        // for availability of the mapping information in further processing.
        postProcessMappings(config);
        config.buildMappings();

as I understand (may be I'm wrong) this buildMapping method parses all classes that specified as mapped classes or placed in packagesToScan and creates Table representation of all this classes. After this called postProcessConfiguration method.

With Hibernate 4 version we don't have such postProcess methods. But we can override buildSessionFactory method like this:

@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初始化之前更改实体模式名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆