【发布时间】:2013-05-02 12:27:01
【问题描述】:
假设我们有一堆实体类,它们在每个人之间都有映射:
@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
}
我们可以使用hibernate中的Configuration类来为一些带注释的类生成创建脚本:
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 添加所有映射?现在我通过注释 Legacy 映射为 NewEntity 生成脚本,然后手动将它们添加回来。
【问题讨论】:
标签: java sql hibernate hibernate-mapping