使用 Hibernate (hbm2ddl) 克隆表的定义 [英] Clone a Table's definition with Hibernate (hbm2ddl)

查看:33
本文介绍了使用 Hibernate (hbm2ddl) 克隆表的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的休眠应用程序中,有一个注释驱动的对象:AuditEvent.它非常简单,没有外键关系.我通过将旧条目移动到另一个表 OldAuditEvent 来存档该表中的旧条目,该表是 AuditEvent 表的克隆.

In my hibernate application there is annotation driven object: AuditEvent. Its very simple and has no foreign key relationships. I archive old entries in this table by moving them to another table OldAuditEvent, which is a clone of the AuditEvent table.

现在我们使用 hbm2ddl(在我们带注释的数据模型上)为整个应用程序生成 DDL,并手动复制/粘贴 AuditEvent 表并更改其名称以创建 OldAuditEvent.

Right now we generate the DDL for the entire application using hbm2ddl (on our annotated datamodel) and manually copy/paste the AuditEvent table and change its name to create OldAuditEvent.

我想自动化构建过程,有什么方法可以告诉 hbb2ddl:嘿,拿这个实体,把表名改成 X 并重新生成它的 DDL"?

I want to automate the build process, is there any way to tell hbb2ddl: "hey take this entity, change the table name to X and regenerate it's DDL"?

更新:我能够通过您概述的方法使其工作.唯一的麻烦是获取 AnnotationSessionFactoryBean,因为它是一个工厂 bean,而 spring 只会给你它工厂的输出.我创建了 ConfigExposingAnnotationSessionFactoryBean(扩展 AnnotationSessionFactoryBean)以通过静态公开 bean 工厂——有点像黑客,但我想做的就是运行构建时任务.

Update: I was able to get this working by the approach you outlined. The only trouble was getting at the AnnotationSessionFactoryBean since it is a factory bean and spring will only give you the output of its factory. I created ConfigExposingAnnotationSessionFactoryBean (extending AnnotationSessionFactoryBean) to expose the bean factory through a static -- sort of a hack but all I want to do is run a build time task.

Configuration cfg = ConfigExposingAnnotationSessionFactoryBean.s_instance.getConfiguration();

PersistentClass pClass = cfg.getClassMapping("com.myco.LoginAttempt");
pClass.getTable().setName("ArchiveLoginAttempt");

Dialect dialect = Dialect.getDialect(ConfigExposingAnnotationSessionFactoryBean.s_instance.getHibernateProperties());

// only output create tables, not indexes or FK
for (String s : cfg.generateSchemaCreationScript( dialect )) {
    if (s.contains("create table") && s.contains("Archive")) {
        m_outstream.print(s);
        m_outstream.println(";");
    }
}

推荐答案

这是可行的,但相当混乱,而且很可能在这种情况下不值得.

It's doable but rather messy and, most likely, not worth it in this case.

您需要动态更改 Hibernate 的 配置 SessionFactory 建立之前的对象.我正在使用 Spring,这可以通过覆盖 AnnotationSessionFactoryBeanpostProcessAnnotationConfiguration() 方法来完成;否则你只需要在调用 buildSessionFactory() 之前使用 Configuration 对象来完成它.

You'll need to dynamically alter Hibernate's Configuration object before SessionFactory is built. I you're using Spring, this can be done by overriding postProcessAnnotationConfiguration() method of AnnotationSessionFactoryBean; otherwise you'll just need to do it using your Configuration object prior to invoking buildSessionFactory() on it.

您可以通过 configuration.getMappings() 访问类/表映射.然后,您需要通过 getTable() 找到您的表映射,通过 addTable() 创建一个具有新名称的副本,并通过 Table API.

You can get access to class / table mappings via configuration.getMappings(). You will then need to find your table mapping via getTable(), create a copy with new name via addTable() and replicate all columns / keys via Table API.

然后您可以通过 Configuration 对象的 generateSchemaCreationScript()generateSchemaUpdateScript() 方法生成 DDL 脚本.

You can then generate the DDL script via generateSchemaCreationScript() or generateSchemaUpdateScript() methods of Configuration object.

正如我所说,在这种情况下可能不值得:-)

As I said, probably not worth it in this case :-)

这篇关于使用 Hibernate (hbm2ddl) 克隆表的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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