我和这个问题的用户有同样的问题https://github.com/jOOQ/jOOQ/issues/8080。当我们生成的 jOOQ 代码与我们的数据库模式不完全匹配时,使用
select(alias.asterisk())
的查询会抛出错误,因为列没有正确排列。
org.postgresql.util.PSQLException: Bad value for type long : 633473bd597addd971f5b27e
at org.postgresql.jdbc.PgResultSet.toLong(PgResultSet.java:3328)
at org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2540)
at com.zaxxer.hikari.pool.HikariProxyResultSet.getLong(HikariProxyResultSet.java)
at org.jooq.tools.jdbc.DefaultResultSet.getLong(DefaultResultSet.java:149)
at org.jooq.impl.CursorImpl$CursorResultSet.getLong(CursorImpl.java:562)
at org.jooq.impl.DefaultBinding$DefaultLongBinding.get0(DefaultBinding.java:3071)
at org.jooq.impl.DefaultBinding$DefaultLongBinding.get0(DefaultBinding.java:3032)
at org.jooq.impl.DefaultBinding$InternalBinding.get(DefaultBinding.java:1071)
at org.jooq.impl.CursorImpl$CursorRecordInitialiser.setValue(CursorImpl.java:1581)
at org.jooq.impl.CursorImpl$CursorRecordInitialiser.apply(CursorImpl.java:1517)
at org.jooq.impl.CursorImpl$CursorRecordInitialiser.apply(CursorImpl.java:1432)
at org.jooq.impl.RecordDelegate.operate(RecordDelegate.java:144)
at org.jooq.impl.CursorImpl$CursorIterator.fetchNext(CursorImpl.java:1389)
at org.jooq.impl.CursorImpl$CursorIterator.hasNext(CursorImpl.java:1365)
at org.jooq.impl.CursorImpl.fetchNext(CursorImpl.java:173)
at org.jooq.impl.AbstractCursor.fetch(AbstractCursor.java:177)
at org.jooq.impl.AbstractCursor.fetch(AbstractCursor.java:88)
at org.jooq.impl.AbstractResultQuery.execute(AbstractResultQuery.java:265)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:357)
at org.jooq.impl.AbstractResultQuery.fetch(AbstractResultQuery.java:290)
at org.jooq.impl.SelectImpl.fetch(SelectImpl.java:2838)
当我使用
.asterisk()
时,查询工作正常。 GitHub 问题列出了两种解决方法,一种使用 TABLE.fields()
,另一种使用 row(...)
。但是,我无法使这些解决方法起作用。要么我在 select 语句上遇到编译错误:
context
.select(MY_TABLE.fields())
...
None of the following functions can be called with the arguments supplied.
select((MutableCollection<out SelectFieldOrAsterisk!>..Collection<SelectFieldOrAsterisk!>?)) defined in org.jooq.DSLContext
select(vararg SelectFieldOrAsterisk!) defined in org.jooq.DSLContext
select(SelectField<TypeVariable(T1)!>!) where T1 = TypeVariable(T1) for fun <T1 : Any!> select(field1: SelectField<T1!>!): SelectSelectStep<Record1<T1!>!> defined in org.jooq.DSLContext
或者我得到所有空值的结果:
private companion object {
private const val MY_TABLE_ALIAS = "table"
private val MY_TABLE = TABLE
.`as`(MY_TABLE_ALIAS)
}
...
context
.select(MY_TABLE.fieldsRow())
.from(MY_TABLE)
.fetchInto(MY_TABLE)
// values are all null in the toApi call
.map(AvailabilityRegionsRecord::toApi)
方言:PostgreSQL jOOQ 版本:3.18.3
我在这里错过了什么,和/或我可以采取不同的方法吗?
刚刚呢
select()
.from(MY_TABLE)
.fetchInto(MY_TABLE)
这会生成仅包含您的 jooq 模式当时知道的列的 SQL。