【问题标题】:Join eq function not working with Jooq and Kotlin加入 eq 函数不适用于 Jooq 和 Kotlin
【发布时间】:2020-09-04 02:29:06
【问题描述】:

我正在使用:

  • Jooq 3.13.2
  • Kotlin 1.3.71
  • Spring Boot 2.2.6.RELESE
  • Java 11

我能够生成 Jooq 类并执行一个简单的查询:

class StoryCustomRepositoryImpl @Autowired constructor(
    private val dslContext: DSLContext
): StoryCustomRepository {
    override fun findEmployeeStories(pageable: Pageable) {
        return dslContext.select(STORY.ID, STORY.DESCRIPTION)
            .from(STORY)
            .forEach { println($it[STORY.ID]) }
    }
}

当我尝试通过添加连接来添加更复杂的逻辑时,编译失败:

class StoryCustomRepositoryImpl @Autowired constructor(
    private val dslContext: DSLContext
): StoryCustomRepository {
    override fun findEmployeeStories(pageable: Pageable) {
        return dslContext.select(STORY.ID, STORY.DESCRIPTION)
            .from(STORY)
            .join(USERS).on(USERS.ID.eq(STORY.CREATED_BY))
            .forEach { println($it[STORY.ID]) }
    }
}

编译在以下行失败.join(USERS).on(USERS.ID.eq(STORY.CREATED_BY))

错误:

None of the following functions can be called with the arguments supplied: 
public abstract fun eq(p0: Int!): Condition! defined in org.jooq.TableField
public abstract fun eq(p0: Field<Int!>!): Condition! defined in org.jooq.TableField
public abstract fun eq(p0: QuantifiedSelect<out Record1<Int!>!>!): Condition! defined in org.jooq.TableField
public abstract fun eq(p0: Select<out Record1<Int!>!>!): Condition! defined in org.jooq.TableField

我正在关注本教程:https://blog.jooq.org/2017/05/18/10-nice-examples-of-writing-sql-in-kotlin-with-jooq/

编辑: 看起来问题是 STORY.CREATED_BY 是 Long 类型,而 USERS.ID 是 Integer 类型。我不确定需要进行哪些更改才能解决此问题。

谢谢

【问题讨论】:

  • CREATED_BY 字段的生成类型是什么?
  • 嗨@LiorH,正如在问题的编辑部分中发布的那样,看起来问题是因为 id 是整数,而 created_by 在数据库中是 biginteger(导致生成的代码对于 id 是整数,对于 createdBy 是 long )。我想我找到了一个使用强制转换方法的临时解决方案:.leftJoin(USERS).on(USERS.ID.cast(Long::class.java).eq(STORY.CREATED_BY)) 我认为更好的解决方案是对齐数据库中的类型。我想我最终会这样做。

标签: kotlin jooq


【解决方案1】:

您可能应该将所有这些ID 列的类型及其引用更改为相同,即BIGINT

作为一种快速解决方法,您可以使用Field.coerce()。我更喜欢Field.cast()。不同之处在于 coerce() 对生成的 SQL 没有任何影响(您希望避免这种影响以获得更好的索引使用),而 cast() 转换为 SQL CAST() 函数。

【讨论】:

  • 谢谢卢卡斯。改成强制就行了。我想知道为什么我的查询没有使用带有强制转换的索引:)
猜你喜欢
  • 2014-09-16
  • 2019-10-13
  • 2012-06-26
  • 1970-01-01
  • 1970-01-01
  • 2017-11-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多