JOOQ Java 8 模式是抽象的,无法实例化

JOOQ Java 8 Schema is Abstract and cannot be instantiated

在 JOOQ documentation 它说我可以这样做:

try (Connection c = getConnection()) {
    String sql = "select schema_name, is_default " +
                 "from information_schema.schemata " +
                 "order by schema_name";

    DSL.using(c)
       .fetch(sql)

       // We can use lambda expressions to map jOOQ Records
       .map(rs -> new Schema(
           rs.getValue("SCHEMA_NAME", String.class),
           rs.getValue("IS_DEFAULT", boolean.class)
       ))

       // ... and then profit from the new Collection methods
       .forEach(System.out::println);
}

然而,当我这样做时,我得到了错误 "org.jooq.Schema is abstract; cannot be instantiated" - 如果你看一下 documentation 那是真的。

那么示例中的代码究竟应该如何工作?

简短回答:他们在示例中没有使用 "org.jooq.Schema",而是静态内部 class.


如果你向下滚动 the page you linked, they give github links to examples. The example you have is the SQL goodies 一个的底部。

如果您打开 SQLGoodies.java,您会注意到示例 class

顶部的静态内部 class Schema
static class Schema {
    final String schemaName;
    final boolean isDefault;

    Schema(String schemaName, boolean isDefault) {
        this.schemaName = schemaName;
        this.isDefault = isDefault;
    }

    @Override
    public String toString() {
        return "Schema{" +
                "schemaName='" + schemaName + '\'' +
                ", isDefault=" + isDefault +
                '}';
    }
}

然后向下滚动,您会找到使用内部 class :

的示例
 DSL.using(c)
     .fetch(sql)
     .map(r -> new Schema(
            r.getValue("SCHEMA_NAME", String.class),
            r.getValue("IS_DEFAULT", boolean.class)
     ))