【问题标题】:Selecting from existing Records with jOOQ使用 jOOQ 从现有记录中进行选择
【发布时间】:2020-02-18 08:36:50
【问题描述】:

是否可以使用 jOOQ 从现有的 Records 创建 VALUES() 表达式而不会丢失元信息?

类似:

    List<ExampleEntityRecord> records = ...

    select()
        .from(values(/* records */))
        .where(EXAMPLE_ENTITY.EXAMPLE_FIELD.eq("some value"))

目前我正在使用values(*records.map { it.valuesRow() }.toTypedArray())。然而,结果是 Table&lt;RecordN&lt;/* ... */&gt;&gt; 没有关于字段的元信息。

【问题讨论】:

    标签: jooq


    【解决方案1】:

    您可以使用DSL.table(Result&lt;R&gt;)DSL.table(R...) 便捷方法,它们在生成的Table&lt;R&gt; 类型中维护&lt;R extends Record&gt; 类型:

    ExampleEntityRecord[] records = ...
    
    select()
        .from(table(records).as(EXAMPLE_ENTITY))
        .where(EXAMPLE_ENTITY.EXAMPLE_FIELD.eq("some value"))
    

    注意,很遗憾,接受Collection&lt;?&gt; 的重载会稍有不同。

    【讨论】:

    • 我注意到的一件事可能与 Kotlin 有关:table(records) 称为 table(Object[] array),它的作用也略有不同。需要 Kotlins 扩展运算符才能做到这一点:table(*records.toTypedArray())
    • 谢谢。我已经走得更远了,但它仍然不起作用。 select() .from(table(*records.toTypedArray())) .where(EXAMPLE_ENTITY.EXAMPLE_FIELD.eq("some value")); 这会产生一个错误:missing FROM-clause entry for table "example_entity"
    • 我试图创建一个别名,但我无法让它工作:ExampleEntityRecord[] records = ...; ExampleEntity tableDef = EXAMPLE_ENTITY.as("val"); select(tableDef.fields()) .from(table(records).as(tableDef)) .where(tableDef.EXAMPLE_FIELD.eq("some value")); 这会产生一个错误column "val.example_field" does not exist。我认为是因为生成的VALUES AS 语句中缺少列名。
    • @JohannesBarop:可能是一个错误。介意在这里报告一个:github.com/jOOQ/jOOQ/issues/new/choose
    猜你喜欢
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2012-12-14
    • 2021-11-02
    • 2017-02-05
    • 2022-01-03
    • 2013-06-12
    相关资源
    最近更新 更多