Apache Metamodel如何在创建表时添加外键

时间:2016-11-13 09:05:38

标签: 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)
            .withColumn("anotherTableId").ofType(ColumnType.INTEGER).execute();
            updateCallback.createTable(schema, "anotherTable").withColumn("id").ofType(ColumnType.INTEGER).execute();
        }
}

如何添加这些表之间的关系?

1 个答案:

答案 0 :(得分:0)

你可以尝试:

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"));
        }
}