您不能使用:
db.Public.PUBLIC.getTables().isEmpty()
Run Code Online (Sandbox Code Playgroud)
因为生成的元信息没有连接到数据库。相反,您可能需要查看DSLContext.meta(). 在您的情况下,您只需编写:
DSL.using(configuration).meta().getTables().isEmpty();
Run Code Online (Sandbox Code Playgroud)
如果您经常运行此测试,这当然不是检查是否有任何表的高效方法,因为它会将所有表提取到内存中只是为了运行isEmpty()检查。我建议改为发出实际查询:
int numberOfTables =
DSL.using(configuration)
.select(count())
.from("information_schema.tables")
.where("table_schema = 'PUBLIC'")
.fetchOne(0, int.class);
Run Code Online (Sandbox Code Playgroud)
未来的 jOOQ 版本(3.11 之后)将能够为您提供可在 SQL 或其他地方使用的实际对象存在谓词:
https://github.com/jOOQ/jOOQ/issues/8038