JOOQ 生成的模型在使用自定义绑定时需要手动导入第三方库
Models generated by JOOQ require manual imports for third party libraries when using custom bindings
我正在为我的 Postgresql 数据库在 JOOQ 中实现 JSONB 支持的自定义绑定,如此处所述:http://www.jooq.org/doc/3.8/manual/code-generation/custom-data-type-bindings/
在 运行 代码生成器上,创建了适当的模型,但是 gradle 给我一个构建错误,因为模型现在需要 JsonElement
导入。我是否需要做一些特定的事情来确保生成的模型导入了适当的库?
谢谢!
编辑:我还有一个 Postgresql 函数,它 returns 一个 JSONB,它在代码生成后得到对应的 Java class。但这也给我一个编译错误。这是屏幕截图:Compile error screenshot
编辑 2:我的整个 gradle 构建配置:
apply plugin: "java"
apply plugin: "application"
mainClassName = "com.vault.server.VaultServer"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
defaultTasks = ['clean', 'build']
sourceSets {
main {
java { srcDir 'src' }
resources { srcDir 'resources' }
}
test {
java { srcDir 'test' }
resources { srcDir 'resources' }
}
integTest {
java.srcDir file('integrationTest')
resources.srcDir file('resources')
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath = output + compileClasspath
}
}
repositories { mavenCentral() }
dependencies {
compile "log4j:log4j:1.2.17"
compile "org.apache.commons:commons-lang3:3.3.2"
compile "org.jooq:jooq:3.8.4"
compile "org.jooq:jooq-meta:3.8.4"
compile "org.jooq:jooq-codegen:3.8.4"
compile "com.google.guava:guava:17.0"
compile "org.apache.httpcomponents:httpclient:4.3.4"
compile "com.fasterxml.jackson.core:jackson-databind:2.4.1.1"
compile "commons-io:commons-io:2.4"
compile "org.eclipse.jetty.aggregate:jetty-all-server:8.1.15.v20140411"
compile "org.yaml:snakeyaml:1.13"
compile "redis.clients:jedis:2.6.0"
compile 'org.apache.commons:commons-collections4:4.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'org.postgresql:postgresql:9.4.1208.jre7'
compile 'org.apache.commons:commons-dbcp2:2.0.1'
compile group: 'javax.ws.rs', name: 'jsr311-api', version: '1.1.1'
compile group: 'com.sun.jersey', name: 'jersey-bundle', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-servlet', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-client', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-grizzly2', version: '1.19'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile group: 'org.glassfish.jersey.containers', name: 'jersey-container-grizzly2-servlet', version: '2.21'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-core', version: '1.19'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-external', version: '1.19'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-http', version: '1.19'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-grizzly2', version: '1.19'
testCompile group: 'org.glassfish.jersey.test-framework.providers', name: 'jersey-test-framework-provider-jetty', version: '2.6'
testCompile group: 'com.sun.jersey.test.framework', name: 'jersey-test-framework', version: '1.0.3.1'
testCompile "org.hamcrest:hamcrest-library:1.3"
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-all:1.8.4"
testCompile "org.easymock:easymock:3.4"
testCompile "cglib:cglib:3.2.4"
testCompile "org.objenesis:objenesis:2.4"
}
configurations {
integTestCompile { extendsFrom testCompile, compile }
integTestRuntime { extendsFrom integTestCompile, testRuntime }
}
jar {
// adds runtime dependencies to jar package
from(configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }) {
// remove all signature files
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
baseName = 'vault'
manifest.attributes("Main-Class": mainClassName);
}
test {
exclude 'com/vault/**'
maxHeapSize '1024m'
minHeapSize '512m'
}
task integTest(type: Test) {
testClassesDir = project.sourceSets.integTest.output.classesDir
classpath = project.sourceSets.integTest.runtimeClasspath
exclude 'com/vault/**'
maxHeapSize '1024m'
minHeapSize '512m'
}
tasks.withType(Test) {
testLogging { events 'passed' }
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug',
'-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=4000'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.0'
}
task generateVersion() {
def git_hash = new ByteArrayOutputStream()
exec{
executable "git"
args "rev-parse", "--verify", "HEAD"
standardOutput = git_hash
}
def version = git_hash.toString()
new File("./resources/version").write(version)
println "Git version is: " + version
}
build.dependsOn generateVersion
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'nu.studer:gradle-jooq-plugin:1.0.5'
classpath 'org.postgresql:postgresql:9.4.1208.jre7' // database-specific JDBC driver
classpath 'com.google.code.gson:gson:2.3.1'
}
}
apply plugin: 'nu.studer.jooq'
jooq {
recordsDb(sourceSets.main) {
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://localhost:5432/'
user = 'postgres'
password = 'xyz'
}
generator {
name = 'org.jooq.util.DefaultGenerator'
strategy {
name = 'org.jooq.util.DefaultGeneratorStrategy'
}
database {
name = 'org.jooq.util.postgres.PostgresDatabase'
schemata {
schema {
inputSchema = "shard_0"
}
}
}
customTypes {
customType {
name = "JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
}
forcedTypes {
forcedType {
name = "JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
}
generate {
relations = true
deprecated = false
records = true
immutablePojos = true
fluentSetters = true
}
target {
packageName = 'com.vault.jooq'
directory = 'target/generated-sources'
}
}
}
}
PostgresJSONGson绑定文件:
package com.vault.dao;
import static org.jooq.tools.Convert.convert;
import java.sql.*;
import java.util.Objects;
import java.util.logging.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.jooq.*;
import org.jooq.impl.DSL;
import com.google.gson.*;
// We're binding <T> = Object (unknown JDBC type), and <U> = JsonElement (user type)
public class PostgresJSONGsonBinding implements Binding<Object, JsonElement> {
// The converter does all the work
@Override
public Converter<Object, JsonElement> converter() {
return new Converter<Object, JsonElement>() {
@Override
public JsonElement from(Object t) {
return t == null ? JsonNull.INSTANCE : new Gson().fromJson("" + t, JsonElement.class);
}
@Override
public Object to(JsonElement u) {
return u == null || u == JsonNull.INSTANCE ? null : new Gson().toJson(u);
}
@Override
public Class<Object> fromType() {
return Object.class;
}
@Override
public Class<JsonElement> toType() {
return JsonElement.class;
}
};
}
// Rending a bind variable for the binding context's value and casting it to the json type
@Override
public void sql(BindingSQLContext<JsonElement> ctx) throws SQLException {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
}
// Registering VARCHAR types for JDBC CallableStatement OUT parameters
@Override
public void register(BindingRegisterContext<JsonElement> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
}
// Converting the JsonElement to a String value and setting that on a JDBC PreparedStatement
@Override
public void set(BindingSetStatementContext<JsonElement> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
}
// Getting a String value from a JDBC ResultSet and converting that to a JsonElement
@Override
public void get(BindingGetResultSetContext<JsonElement> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
// Getting a String value from a JDBC CallableStatement and converting that to a JsonElement
@Override
public void get(BindingGetStatementContext<JsonElement> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
// Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types)
@Override
public void set(BindingSetSQLOutputContext<JsonElement> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
// Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types)
@Override
public void get(BindingGetSQLInputContext<JsonElement> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
自定义类型配置
您发现缺少导入的原因在这里:
customTypes {
customType {
// Lacking a "type" definition, jOOQ uses the "name" as the "type":
name = "JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
}
forcedTypes {
forcedType {
name = "JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
}
因为你只指定了 customType.name
属性(而不是 customType.type
属性),jOOQ 的代码生成器使用 name
作为类型信息。但是 jOOQ 不知道那个类型是什么意思或者它在哪里。 IE。你有三个选项来解决这个问题:
1.添加类型
customType {
type = "com.google.gson.JsonElement"
name = "JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
2。用限定类型替换名称
customTypes {
customType {
name = "com.google.gson.JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
}
forcedTypes {
forcedType {
name = "com.google.gson.JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
}
3。使用jOOQ 3.8的简化强制类型配置
如 the jOOQ 3.8+ manual 中所述,您可以省略使用 customType
配置并仅使用 forcedType
:
forcedType {
userType = "com.google.gson.JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
以上任何一项都可以解决导入问题。
关于 jOOQ 版本的旁注
您的屏幕截图表明您使用的是 jOOQ 版本 3.4.1。请升级到最新版本(目前为3.8.4)。这方面有很多错误修正。
您的 nu.studer:gradle-jooq-plugin:1.0.5
版本似乎正在拉取版本 3.4.1。参见:
https://github.com/etiennestuder/gradle-jooq-plugin/blob/1.0.5/gradle.properties
你可以升级到1.0.6
,它会拉取3.6.2,这会好一点,但仍然不是最新版本。
我怀疑这是第三方 Gradle 插件中需要修复的错误/限制。我为此注册了一个问题:
https://github.com/etiennestuder/gradle-jooq-plugin/issues/18
我正在为我的 Postgresql 数据库在 JOOQ 中实现 JSONB 支持的自定义绑定,如此处所述:http://www.jooq.org/doc/3.8/manual/code-generation/custom-data-type-bindings/
在 运行 代码生成器上,创建了适当的模型,但是 gradle 给我一个构建错误,因为模型现在需要 JsonElement
导入。我是否需要做一些特定的事情来确保生成的模型导入了适当的库?
谢谢!
编辑:我还有一个 Postgresql 函数,它 returns 一个 JSONB,它在代码生成后得到对应的 Java class。但这也给我一个编译错误。这是屏幕截图:Compile error screenshot
编辑 2:我的整个 gradle 构建配置:
apply plugin: "java"
apply plugin: "application"
mainClassName = "com.vault.server.VaultServer"
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
defaultTasks = ['clean', 'build']
sourceSets {
main {
java { srcDir 'src' }
resources { srcDir 'resources' }
}
test {
java { srcDir 'test' }
resources { srcDir 'resources' }
}
integTest {
java.srcDir file('integrationTest')
resources.srcDir file('resources')
compileClasspath += sourceSets.main.output + sourceSets.test.output
runtimeClasspath = output + compileClasspath
}
}
repositories { mavenCentral() }
dependencies {
compile "log4j:log4j:1.2.17"
compile "org.apache.commons:commons-lang3:3.3.2"
compile "org.jooq:jooq:3.8.4"
compile "org.jooq:jooq-meta:3.8.4"
compile "org.jooq:jooq-codegen:3.8.4"
compile "com.google.guava:guava:17.0"
compile "org.apache.httpcomponents:httpclient:4.3.4"
compile "com.fasterxml.jackson.core:jackson-databind:2.4.1.1"
compile "commons-io:commons-io:2.4"
compile "org.eclipse.jetty.aggregate:jetty-all-server:8.1.15.v20140411"
compile "org.yaml:snakeyaml:1.13"
compile "redis.clients:jedis:2.6.0"
compile 'org.apache.commons:commons-collections4:4.0'
compile 'com.google.code.gson:gson:2.3.1'
compile 'org.postgresql:postgresql:9.4.1208.jre7'
compile 'org.apache.commons:commons-dbcp2:2.0.1'
compile group: 'javax.ws.rs', name: 'jsr311-api', version: '1.1.1'
compile group: 'com.sun.jersey', name: 'jersey-bundle', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-server', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-core', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-servlet', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-client', version: '1.19'
compile group: 'com.sun.jersey', name: 'jersey-grizzly2', version: '1.19'
compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
testCompile group: 'org.glassfish.jersey.containers', name: 'jersey-container-grizzly2-servlet', version: '2.21'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-core', version: '1.19'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-external', version: '1.19'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-http', version: '1.19'
testCompile group: 'com.sun.jersey.jersey-test-framework', name: 'jersey-test-framework-grizzly2', version: '1.19'
testCompile group: 'org.glassfish.jersey.test-framework.providers', name: 'jersey-test-framework-provider-jetty', version: '2.6'
testCompile group: 'com.sun.jersey.test.framework', name: 'jersey-test-framework', version: '1.0.3.1'
testCompile "org.hamcrest:hamcrest-library:1.3"
testCompile "junit:junit:4.11"
testCompile "org.mockito:mockito-all:1.8.4"
testCompile "org.easymock:easymock:3.4"
testCompile "cglib:cglib:3.2.4"
testCompile "org.objenesis:objenesis:2.4"
}
configurations {
integTestCompile { extendsFrom testCompile, compile }
integTestRuntime { extendsFrom integTestCompile, testRuntime }
}
jar {
// adds runtime dependencies to jar package
from(configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }) {
// remove all signature files
exclude "META-INF/*.SF"
exclude "META-INF/*.DSA"
exclude "META-INF/*.RSA"
}
baseName = 'vault'
manifest.attributes("Main-Class": mainClassName);
}
test {
exclude 'com/vault/**'
maxHeapSize '1024m'
minHeapSize '512m'
}
task integTest(type: Test) {
testClassesDir = project.sourceSets.integTest.output.classesDir
classpath = project.sourceSets.integTest.runtimeClasspath
exclude 'com/vault/**'
maxHeapSize '1024m'
minHeapSize '512m'
}
tasks.withType(Test) {
testLogging { events 'passed' }
if (System.getProperty('DEBUG', 'false') == 'true') {
jvmArgs '-Xdebug',
'-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=4000'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '2.0'
}
task generateVersion() {
def git_hash = new ByteArrayOutputStream()
exec{
executable "git"
args "rev-parse", "--verify", "HEAD"
standardOutput = git_hash
}
def version = git_hash.toString()
new File("./resources/version").write(version)
println "Git version is: " + version
}
build.dependsOn generateVersion
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'nu.studer:gradle-jooq-plugin:1.0.5'
classpath 'org.postgresql:postgresql:9.4.1208.jre7' // database-specific JDBC driver
classpath 'com.google.code.gson:gson:2.3.1'
}
}
apply plugin: 'nu.studer.jooq'
jooq {
recordsDb(sourceSets.main) {
jdbc {
driver = 'org.postgresql.Driver'
url = 'jdbc:postgresql://localhost:5432/'
user = 'postgres'
password = 'xyz'
}
generator {
name = 'org.jooq.util.DefaultGenerator'
strategy {
name = 'org.jooq.util.DefaultGeneratorStrategy'
}
database {
name = 'org.jooq.util.postgres.PostgresDatabase'
schemata {
schema {
inputSchema = "shard_0"
}
}
}
customTypes {
customType {
name = "JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
}
forcedTypes {
forcedType {
name = "JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
}
generate {
relations = true
deprecated = false
records = true
immutablePojos = true
fluentSetters = true
}
target {
packageName = 'com.vault.jooq'
directory = 'target/generated-sources'
}
}
}
}
PostgresJSONGson绑定文件:
package com.vault.dao;
import static org.jooq.tools.Convert.convert;
import java.sql.*;
import java.util.Objects;
import java.util.logging.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.jooq.*;
import org.jooq.impl.DSL;
import com.google.gson.*;
// We're binding <T> = Object (unknown JDBC type), and <U> = JsonElement (user type)
public class PostgresJSONGsonBinding implements Binding<Object, JsonElement> {
// The converter does all the work
@Override
public Converter<Object, JsonElement> converter() {
return new Converter<Object, JsonElement>() {
@Override
public JsonElement from(Object t) {
return t == null ? JsonNull.INSTANCE : new Gson().fromJson("" + t, JsonElement.class);
}
@Override
public Object to(JsonElement u) {
return u == null || u == JsonNull.INSTANCE ? null : new Gson().toJson(u);
}
@Override
public Class<Object> fromType() {
return Object.class;
}
@Override
public Class<JsonElement> toType() {
return JsonElement.class;
}
};
}
// Rending a bind variable for the binding context's value and casting it to the json type
@Override
public void sql(BindingSQLContext<JsonElement> ctx) throws SQLException {
ctx.render().visit(DSL.val(ctx.convert(converter()).value())).sql("::json");
}
// Registering VARCHAR types for JDBC CallableStatement OUT parameters
@Override
public void register(BindingRegisterContext<JsonElement> ctx) throws SQLException {
ctx.statement().registerOutParameter(ctx.index(), Types.VARCHAR);
}
// Converting the JsonElement to a String value and setting that on a JDBC PreparedStatement
@Override
public void set(BindingSetStatementContext<JsonElement> ctx) throws SQLException {
ctx.statement().setString(ctx.index(), Objects.toString(ctx.convert(converter()).value(), null));
}
// Getting a String value from a JDBC ResultSet and converting that to a JsonElement
@Override
public void get(BindingGetResultSetContext<JsonElement> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.resultSet().getString(ctx.index()));
}
// Getting a String value from a JDBC CallableStatement and converting that to a JsonElement
@Override
public void get(BindingGetStatementContext<JsonElement> ctx) throws SQLException {
ctx.convert(converter()).value(ctx.statement().getString(ctx.index()));
}
// Setting a value on a JDBC SQLOutput (useful for Oracle OBJECT types)
@Override
public void set(BindingSetSQLOutputContext<JsonElement> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
// Getting a value from a JDBC SQLInput (useful for Oracle OBJECT types)
@Override
public void get(BindingGetSQLInputContext<JsonElement> ctx) throws SQLException {
throw new SQLFeatureNotSupportedException();
}
}
自定义类型配置
您发现缺少导入的原因在这里:
customTypes {
customType {
// Lacking a "type" definition, jOOQ uses the "name" as the "type":
name = "JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
}
forcedTypes {
forcedType {
name = "JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
}
因为你只指定了 customType.name
属性(而不是 customType.type
属性),jOOQ 的代码生成器使用 name
作为类型信息。但是 jOOQ 不知道那个类型是什么意思或者它在哪里。 IE。你有三个选项来解决这个问题:
1.添加类型
customType {
type = "com.google.gson.JsonElement"
name = "JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
2。用限定类型替换名称
customTypes {
customType {
name = "com.google.gson.JsonElement"
converter = "com.vault.dao.PostgresJSONGsonBinding"
}
}
forcedTypes {
forcedType {
name = "com.google.gson.JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
}
3。使用jOOQ 3.8的简化强制类型配置
如 the jOOQ 3.8+ manual 中所述,您可以省略使用 customType
配置并仅使用 forcedType
:
forcedType {
userType = "com.google.gson.JsonElement"
expression = ".*data.*|.*tags.*"
types = ".*"
}
以上任何一项都可以解决导入问题。
关于 jOOQ 版本的旁注
您的屏幕截图表明您使用的是 jOOQ 版本 3.4.1。请升级到最新版本(目前为3.8.4)。这方面有很多错误修正。
您的 nu.studer:gradle-jooq-plugin:1.0.5
版本似乎正在拉取版本 3.4.1。参见:
https://github.com/etiennestuder/gradle-jooq-plugin/blob/1.0.5/gradle.properties
你可以升级到1.0.6
,它会拉取3.6.2,这会好一点,但仍然不是最新版本。
我怀疑这是第三方 Gradle 插件中需要修复的错误/限制。我为此注册了一个问题: https://github.com/etiennestuder/gradle-jooq-plugin/issues/18