假设我们有一堆实体类,它们之间有映射:
@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
的所有映射?现在,我通过注释旧映射来生成 NewEntity 的脚本,然后手动将它们添加回来。
最佳答案
如果您的 NewEntity 在其映射关系中引用并与旧对象交互,您需要映射它。
如果 hibernate 操作尚未映射,那么它们如何工作?
如果您的意思是需要现有架构的更新脚本而不是生成新的创建数据库脚本,请尝试 generateSchemaUpdateScript
方法。
关于java - 生成创建脚本时忽略一些实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16437885/