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

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

问题描述

在我的Hibernate应用程序有注解驱动的对象: AuditEvent 的。它非常简单,并没有外键关系。我通过移动到另一个表的 OldAuditEvent 的,这是的 AuditEvent 的表的克隆存档在此表中的旧条目。

现在我们生成DDL使用先得用hbm2ddl(在我们的数据模型注明),整个应用程序和手工复制/粘贴AuditEvent表并更改其名称以创建的 OldAuditEvent 的。

我要自动生成过程中,有没有办法告诉hbb2ddl:哎借此实体,改表名X和再生它的DDL

更新
我可以通过你介绍的方法来得到这个工作。唯一的麻烦是在AnnotationSessionFactoryBean得到,因为它是一个工厂类和春季只会给你的工厂的产量。我创建ConfigExposingAnnotationSessionFactoryBean(扩大AnnotationSessionFactoryBean)通过静态暴露bean工厂 - 有点一个黑客攻击,但所有我想要做的就是运行构建时任务

 配置CFG = ConfigExposingAnnotationSessionFactoryBean.s_instance.getConfiguration();对PersistentClass pClass = cfg.getClassMapping(com.myco.LoginAttempt);
pClass.getTable()的setName(ArchiveLoginAttempt);方言方言= Dialect.getDialect(ConfigExposingAnnotationSessionFactoryBean.s_instance.getHibernateProperties());//只能输出创建表,没有索引或FK
对于(一个String:cfg.generateSchemaCreationScript(方言)){
如果(s.contains(创建表)及和放大器; s.contains(归档)){
m_outstream.print(多个);
m_outstream.println(;);
}
}


解决方案

这是可行的,但相当凌乱,最有可能的,不值得在这种情况下。

您需要动态改变Hibernate的配置的SessionFactory对象之前建成。我你使用Spring,这可以通过重写的 postProcessAnnotationConfiguration()方法来实现 AnnotationSessionFactoryBean ;否则你将只需要使用配置对象做调用之前 buildSessionFactory()就可以了。

您可以通过 configuration.getMappings可以访问类/表映射()。然后,您将需要通过来找到你的表映射的GetTable(),通过 addTable()创建一个新名称的副本和复制所有列/通过表API

您可以再生成通过 DDL脚本generateSchemaCreationScript() generateSchemaUpdateScript() <$ C $的方法C>配置对象。

正如我所说的,大概不值得在这种情况下:-)

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

解决方案

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

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

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