使用 Hibernate (hbm2ddl) 克隆表的定义
在我的休眠应用程序中,有一个注释驱动的对象: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是可行的,但相当混乱,而且在这种情况下很可能不值得。
您需要动态更改 Hibernate 的配置 SessionFactory 构建之前的对象。 如果您使用 Spring,这可以通过覆盖
AnnotationSessionFactoryBean
的postProcessAnnotationConfiguration()
方法来完成; 否则,您只需要在调用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 ofAnnotationSessionFactoryBean
; otherwise you'll just need to do it using yourConfiguration
object prior to invokingbuildSessionFactory()
on it.You can get access to class / table mappings via
configuration.getMappings()
. You will then need to find your table mapping viagetTable()
, create a copy with new name viaaddTable()
and replicate all columns / keys via Table API.You can then generate the DDL script via
generateSchemaCreationScript()
orgenerateSchemaUpdateScript()
methods ofConfiguration
object.As I said, probably not worth it in this case :-)