[英]JOOQ - column “id” is of type uuid but expression is of type character varying
I'm having an issue related to JOOQ. 我遇到与JOOQ相关的问题。
--------- ---------
This is my "setup" that led to the issue. 这是导致问题的我的“设置”。
Table: 表:
CREATE TABLE "public".xyz
(
id UUID NOT NULL,
CONSTRAINT pk_t_xyz PRIMARY KEY(id)
);
Generated field with JOOQ is correct JOOQ生成的字段正确
public final TableField<XYZRecord, UUID> ID = createField("id", org.jooq.impl.SQLDataType.UUID.nullable(false), this, "comment");
UUID is from java.util.*
UUID来自
java.util.*
My "custom" POJO with UUID from java.util.*
: 我的“自定义” POJO具有来自
java.util.*
UUID:
public class XYZ {
@NotNull
private UUID id;
public XYZ (@NotNull UUID id) {
this.id = id;
}
public UUID getId() {
return id;
}
}
DSL configuration: DSL配置:
@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();
}
}
Datasource in application.yml application.yml中的数据源
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/xyz-data
Version of jooq is 3.10.5. jooq的版本是3.10.5。 I'm using
spring-boot-starter-jooq
with spring boot in version 2.0.0.RELEASE
. 我在
spring-boot-starter-jooq
版本2.0.0.RELEASE
spring-boot-starter-jooq
与spring boot 2.0.0.RELEASE
。 PostgresSQL version is 10. PostgresSQL版本为10。
------------------ ------------------
When I try to insert data like this: 当我尝试像这样插入数据时:
dslContext.insertInto(XYZ, XYZ.ID)
.values(xyz.getId()).execute();
It does not work for some reason. 由于某种原因,它不起作用。 As I understand from exception below JOOQ is casting UUID to string and therefore the SQL is invalid.
从下面的异常可以理解,JOOQ正在将UUID强制转换为字符串,因此SQL无效。 Should I write some kind of converter or did I define something in wrong way?
我应该编写某种转换器还是以错误的方式定义某些内容?
Error: 错误:
org.springframework.jdbc.BadSqlGrammarException: Access database using jOOQ; bad SQL grammar [insert into "public"."xyz" ("id") values (?)]; nested exception is org.postgresql.util.PSQLException: ERROR: column "id" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 225
[...]
Caused by: org.postgresql.util.PSQLException: ERROR: column "id" is of type uuid but expression is of type character varying
Hint: You will need to rewrite or cast the expression.
Position: 225
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2422)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2167)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:306)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:441)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:365)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:155)
at org.postgresql.jdbc.PgPreparedStatement.execute(PgPreparedStatement.java:144)
at com.zaxxer.hikari.pool.ProxyPreparedStatement.execute(ProxyPreparedStatement.java:44)
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.execute(HikariProxyPreparedStatement.java)
at org.jooq.tools.jdbc.DefaultPreparedStatement.execute(DefaultPreparedStatement.java:209)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:429)
at org.jooq.impl.AbstractDMLQuery.execute(AbstractDMLQuery.java:452)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:347)
... 51 more
I had this exact issue with Spring Boot. 我在Spring Boot中遇到了这个确切的问题。 I could work around the issue by explicitly specifying the Postgres Dialect when setting up the DefaultConfiguration object.
我可以通过在设置DefaultConfiguration对象时显式指定Postgres方言来解决此问题。
Eg in your DSL configuration: 例如,在您的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;
}
This is a bug in jOOQ: https://github.com/jOOQ/jOOQ/issues/7351 这是jOOQ中的错误: https : //github.com/jOOQ/jOOQ/issues/7351
It seems to happen only when binding null
values as UUIDs. 似乎仅当将
null
值绑定为UUID时才会发生。 The workaround is to implement your own data type binding: https://www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings 解决方法是实现您自己的数据类型绑定: https : //www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.