[英]Column is of type uuid but expression is of type character varying in Spark Scala
[英]JOOQ - column “id” is of type uuid but expression is of type character varying
我遇到與JOOQ相關的問題。
---------
這是導致問題的我的“設置”。
表:
CREATE TABLE "public".xyz
(
id UUID NOT NULL,
CONSTRAINT pk_t_xyz PRIMARY KEY(id)
);
JOOQ生成的字段正確
public final TableField<XYZRecord, UUID> ID = createField("id", org.jooq.impl.SQLDataType.UUID.nullable(false), this, "comment");
UUID來自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
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();
}
}
application.yml中的數據源
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/xyz-data
jooq的版本是3.10.5。 我在spring-boot-starter-jooq
版本2.0.0.RELEASE
spring-boot-starter-jooq
與spring boot 2.0.0.RELEASE
。 PostgresSQL版本為10。
------------------
當我嘗試像這樣插入數據時:
dslContext.insertInto(XYZ, XYZ.ID)
.values(xyz.getId()).execute();
由於某種原因,它不起作用。 從下面的異常可以理解,JOOQ正在將UUID強制轉換為字符串,因此SQL無效。 我應該編寫某種轉換器還是以錯誤的方式定義某些內容?
錯誤:
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
我在Spring Boot中遇到了這個確切的問題。 我可以通過在設置DefaultConfiguration對象時顯式指定Postgres方言來解決此問題。
例如,在您的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;
}
這是jOOQ中的錯誤: https : //github.com/jOOQ/jOOQ/issues/7351
似乎僅當將null
值綁定為UUID時才會發生。 解決方法是實現您自己的數據類型綁定: https : //www.jooq.org/doc/latest/manual/sql-building/queryparts/custom-bindings
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.