使用 Hibernate (hbm2ddl) 克隆表的定义

发布于 2024-07-29 20:45:58 字数 1184 浏览 2 评论 0原文

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

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

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

更新: 我能够通过您概述的方法来实现此目的。 唯一的麻烦是获取 AnnotationSessionFactoryBean,因为它是一个工厂 bean,spring 只会给你它工厂的输出。 我创建了 ConfigExusingAnnotationSessionFactoryBean (扩展 AnnotationSessionFactoryBean)来通过静态公开 Bean 工厂——有点黑客,但我想做的就是运行构建时任务。

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

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.

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.

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"?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物 2024-08-05 20:45:58

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

您需要动态更改 Hibernate 的配置 SessionFactory 构建之前的对象。 如果您使用 Spring,这可以通过覆盖 AnnotationSessionFactoryBeanpostProcessAnnotationConfiguration() 方法来完成; 否则,您只需要在调用 buildSessionFactory() 之前使用 Configuration 对象来完成此操作。

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

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

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

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

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.

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.

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 :-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文