假设我们有一堆实体类,它们在每个人之间都有映射:
@Entity
@Table(name = "legacy")
public class Legacy {
// Mappings to a bunch of other different Entities
}
@Entity
@Table(name = "new_entity")
public class NewEntity {
private Legacy legacy;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "legacy_id", referencedColumnName = "id")
public Legacy getLegacy() {
return legacy;
}
public Legacy setLegacy(Legacy legacy) {
this.legacy = legacy;
}
// Mappings to other new stuff
}
我们可以使用Configuration
hibernate中的类来为一些带注释的类生成创建脚本:
Configuration config = new Configuration();
Properties properties = new Properties();
properties.put("hibernate.dialect", "org.hibernate.dialect.SQLServer2005Dialect");
config.setProperties(properties);
config.addAnnotatedClass(NewEntity.class)
String[] schema =
config.generateSchemaCreationScript(new SQLServer2005Dialect());
for (String table : schema) {
System.out.println(table);
}
但是,这将失败,因为该类Legacy
尚未添加到配置中。但是,如果我这样做,我需要添加一堆其他遗留类(它们都已经具有“工作”映射和表。
有没有办法只为 生成脚本NewEntity
而不必添加所有映射Legacy
?现在,我通过注释旧版映射为 NewEntity 生成脚本,然后手动将它们添加回来。