JOOQ - 列“id”的类型为uuid,但表达式的类型为字符变化

时间:2018-03-26 11:36:39

标签: java spring postgresql jooq

我遇到了与JOOQ有关的问题。

---------
这是导致问题的“设置” 表:

This is the code surrounding the buttons,
<a href="/?do_Action=ViewEntity&amp;Entity_ID=14287">View</a> 
<a href="/?do_Action=DecomRequest&amp;Entity_ID=14287">Decom</a> 
<a href="/?do_Action=ViewServerLog&amp;Entity_ID=14287">Log</a></td>

JOOQ生成的字段正确

CREATE TABLE "public".xyz
(
  id UUID NOT NULL,
  CONSTRAINT pk_t_xyz PRIMARY KEY(id)
);

UUID来自public final TableField<XYZRecord, UUID> ID = createField("id", org.jooq.impl.SQLDataType.UUID.nullable(false), this, "comment");

我的“自定义”POJO,来自java.util.*的UUID:

java.util.*

DSL配置:

public class XYZ {

    @NotNull
    private UUID id;

    public XYZ (@NotNull UUID id) {
        this.id = id;
    }

    public UUID getId() {
        return id;
    }
}

application.yml中的数据源

@Configuration
public class DataSourceConfiguration {

    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;

    @Bean
    public DefaultDSLContext dsl() {
        return new DefaultDSLContext(configuration());
    }

    @Bean
    public DataSourceConnectionProvider connectionProvider() {
        return new DataSourceConnectionProvider
            (new TransactionAwareDataSourceProxy(dataSource));
    }

    public DefaultConfiguration configuration() {
        DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
        jooqConfiguration.set(connectionProvider());
        jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
        return jooqConfiguration;
    }

    @Bean
    public ExceptionTranslator exceptionTransformer() {
        return new ExceptionTranslator();
    }
}

jooq的版本是3.10.5。我在版本spring: datasource: type: com.zaxxer.hikari.HikariDataSource driver-class-name: org.postgresql.Driver url: jdbc:postgresql://localhost:5432/xyz-data 中使用spring-boot-starter-jooq进行弹簧启动。 PostgresSQL版本是10.
------------------
当我尝试插入这样的数据时:

2.0.0.RELEASE

由于某种原因它不起作用。正如我从异常中理解的那样,JOOQ将UUID转换为字符串,因此SQL无效。我应该编写某种转换器还是以错误的方式定义某些内容?

错误:

    dslContext.insertInto(XYZ, XYZ.ID)
        .values(xyz.getId()).execute();

2 个答案:

答案 0 :(得分:4)

我在Spring Boot中遇到了这个问题。我可以通过在设置DefaultConfiguration对象时显式指定Postgres Dialect来解决此问题。

E.g。在您的DSL配置中:

public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();

    // Explicitly set the Dialect
    jooqConfiguration.setSQLDialect(SQLDialect.POSTGRES);

    jooqConfiguration.set(connectionProvider());
    jooqConfiguration.set(new DefaultExecuteListenerProvider(exceptionTransformer()));
    return jooqConfiguration;
}

答案 1 :(得分:2)

这是jOOQ中的错误:https://github.com/jOOQ/jOOQ/issues/7351

似乎仅在将{{1}}值绑定为UUID时才会发生。解决方法是实现您自己的数据类型绑定: https://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings