Java Apache元模型如何在创建表时添加外键
我编写了一段代码,通过apache元模型创建一些表:Java Apache元模型如何在创建表时添加外键,java,foreign-keys,apache-metamodel,Java,Foreign Keys,Apache Metamodel,我编写了一段代码,通过apache元模型创建一些表: dataContext.executeUpdate(new UpdateScript() { @Override public void run(UpdateCallback updateCallback) { updateCallback.createTable(schema, "aTable").withColumn("id").ofType(ColumnType.INTEGER)
dataContext.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback updateCallback) {
updateCallback.createTable(schema, "aTable").withColumn("id").ofType(ColumnType.INTEGER)
.withColumn("anotherTableId").ofType(ColumnType.INTEGER).execute();
updateCallback.createTable(schema, "anotherTable").withColumn("id").ofType(ColumnType.INTEGER).execute();
}
}
如何添加这些表之间的关系?您可以尝试:
dataContext.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback updateCallback) {
Table aTable = updateCallback.createTable(schema, "aTable")
.withColumn("id").ofType(ColumnType.INTEGER)
.withColumn("anotherTableId").ofType(ColumnType.INTEGER).execute();
Table anotherTable = updateCallback.createTable(schema, "anotherTable")
.withColumn("id").ofType(ColumnType.INTEGER).execute();
MutableRelationship.createRelationship(
anotherTable.getColumnByName("id"),
aTable.getColumnByName("anotherTableId"));
}
}
你可以试试:
dataContext.executeUpdate(new UpdateScript() {
@Override
public void run(UpdateCallback updateCallback) {
Table aTable = updateCallback.createTable(schema, "aTable")
.withColumn("id").ofType(ColumnType.INTEGER)
.withColumn("anotherTableId").ofType(ColumnType.INTEGER).execute();
Table anotherTable = updateCallback.createTable(schema, "anotherTable")
.withColumn("id").ofType(ColumnType.INTEGER).execute();
MutableRelationship.createRelationship(
anotherTable.getColumnByName("id"),
aTable.getColumnByName("anotherTableId"));
}
}
不幸的是,MutableRelationship.CraeterRelationship()只更新元模型而不是数据库后端。不幸的是,MutableRelationship.CraeterRelationship()只更新元模型而不是数据库后端。