【发布时间】:2011-01-31 11:54:18
【问题描述】:
我使用 Hibernate 作为我的应用程序的 ORM。我想知道当这些升级由其他人完成时,是否有一个很好的解决方案来处理我的应用程序中的模式升级。例如,我有一组使用 Hibernate 工具生成的 hbm.xml 文件和相应的 java 类。现在在生产中,一切正常,直到数据库模式升级(表/列可能被删除/添加)。我没有(我的应用程序没有)访问权限,那么我该如何使用 Hibernate 来处理这个问题?
谢谢!
【问题讨论】:
我使用 Hibernate 作为我的应用程序的 ORM。我想知道当这些升级由其他人完成时,是否有一个很好的解决方案来处理我的应用程序中的模式升级。例如,我有一组使用 Hibernate 工具生成的 hbm.xml 文件和相应的 java 类。现在在生产中,一切正常,直到数据库模式升级(表/列可能被删除/添加)。我没有(我的应用程序没有)访问权限,那么我该如何使用 Hibernate 来处理这个问题?
谢谢!
【问题讨论】:
我想知道当其他人完成这些升级时,是否有一个很好的解决方案来处理我的应用程序中的架构升级。
没有神奇的解决方案,您需要使映射与架构保持同步。所以你有两个选择:
我会选择第二个选项,它的侵入性更小,并且给你更多的控制权(没有大爆炸,你可以保留旧的 getter/setter,你可以弃用一些东西)。
在这两种情况下,沟通都是关键,您需要与做出改变的人一起工作(反之亦然)。
【讨论】:
我一直更喜欢相反的方式 - 只更改类并将 hibernate.hbm2ddl.auto 设置为 update。这样,数据库将根据您的类进行更新。这很有意义,因为您的对象是应用程序的中心,而不是它们的数据库表示。
【讨论】:
这是我解决问题的方法
我使用遗留系统 (Oracle) 其他系统依赖它。为了使用它,DBA(不是我)创建了一个具有只读访问权限的数据库链接。因此,如果我需要为新系统创建一个新实体而不损坏(并且担心)构建在我们遗留系统之上的数据库,我会执行以下操作
假设这里是我们的 legacy 类
@Entity
public class LegacySystemClass {
private Integer id;
/**
* I have read-only access privilege
* So insertable=false, updatable=false
*/
@Id
@Column(name="LEGACY_SYSTEM_ID", insertable=false, updatable=false)
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
我需要为依赖于 LegacyClass 的新系统创建一个类
@Entity
public class NewSystemClass {
private Integer id;
private LegacySystemClass legacySystemClass;
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToOne
@JoinColumn(name="LEGACY_SYSTEM_ID")
public LegacySystemClass getLegacySystemClass() {
return legacySystemClass;
}
public void setLegacySystemClass(LegacySystemClass legacySystemClass) {
this.legacySystemClass = legacySystemClass;
}
}
现在,我根据
生成一个SQL文件AnnotationConfiguration configuration = new AnnotationConfiguration();
configuration
.addAnnotatedClass(NewSystemClass.class)
.addAnnotatedClass(LegacyClass.class)
.setProperty(Environment.DIALECT, <TYPE_YOUR_DIALECT>)
.setProperty(Environment.DRIVER, <TYPE_YOUR_DRIVER>);
SchemaExport schema = new SchemaExport(configuration);
schema.setOutputFile("schema.sql");
/*
schema.create(<DO_YOU_WANT_TO_PRINT_TO_THE_CONSOLE>, <DO_YOU_WANT_TO_EXPORT_THE_SCRIPT_TO_THE_DATABASE>);
*/
/**
* Make sure set up second parameter as false
* See above
*/
schema.create(true, false);
它将生成一个名为 schema.sql 的文件,其中包含
create table LegacySystemClass (LEGACY_SYSTEM_ID integer not null, primary key (LEGACY_SYSTEM_ID))
create table NewSystemClass (id integer not null, LEGACY_SYSTEM_ID integer, primary key (id))
alter table NewSystemClass add index FK8533D2E95B9B5D88 (LEGACY_SYSTEM_ID), add constraint FK8533D2E95B9B5D88 foreign key (LEGACY_SYSTEM_ID) references LegacySystemClass
我提取与我们的遗留系统相关的任何 SQL,例如 LegacyClass。我们最终的schema.sql如下所示
create table NewSystemClass (id integer not null, LEGACY_SYSTEM_ID integer, primary key (id))
alter table NewSystemClass add index FK8533D2E95B9B5D88 (LEGACY_SYSTEM_ID), add constraint FK8533D2E95B9B5D88 foreign key (LEGACY_SYSTEM_ID) references LegacySystemClass (LEGACY_SYSTEM_ID)
我去DBA房间问他
你能帮我运行一下这个 schema.sql 文件吗???
但正如@Pascal Thivent 所说
沟通是关键(极限编程原理-XP)
【讨论】: