【问题标题】:What is the benefit of using primarykey and references method in class jooq在类jooq中使用primarykey和references方法有什么好处
【发布时间】:2019-02-02 03:09:41
【问题描述】:

我开始学习 jooq。我有 mssql 服务器。我在我的服务器上创建了一些代表表的类。但是我不明白在我的表类中使用 getPrimaryKeygetReferences 方法有什么好处?

class User : TableImpl<Record>("users") {

    companion object {
        val USER = User()
    }

    val id: TableField<Record, Int> = createField("id", SQLDataType.INTEGER)
    val name: TableField<Record, String> = createField("name", SQLDataType.NVARCHAR(50))
    val countryId: TableField<Record, Short> = createField("country_id", SQLDataType.SMALLINT)

    override fun getPrimaryKey(): UniqueKey<Record> = Internal.createUniqueKey(this, id)

    override fun getReferences(): MutableList<ForeignKey<Record, *>> =
        mutableListOf(Internal.createForeignKey(primaryKey, COUNTRY, COUNTRY.id))

}

class Country : TableImpl<Record>("country") {

    companion object {
        val COUNTRY = Country()
    }

    val id: TableField<Record, Short> = createField("id", SQLDataType.SMALLINT)
    val name: TableField<Record, String> = createField("name", SQLDataType.NVARCHAR(100))

    override fun getPrimaryKey(): UniqueKey<Record> =
        Internal.createUniqueKey(this, id)

}

【问题讨论】:

    标签: kotlin jooq


    【解决方案1】:

    生成的元数据是有用的东西的混合......

    • ,API 用户
    • jOOQ,它可以反映该元数据的一些内部功能

    例如,在 getPrimaryKey() 的情况下,该方法有助于各种与 CRUD 相关的操作,如您在手册中所见: https://www.jooq.org/doc/latest/manual/sql-execution/crud-with-updatablerecords/simple-crud

    如果您没有使用代码生成器(它将为您生成所有这些方法),则无需将它们添加到您的类中。您可以将它们缩短为:

    class User : TableImpl<Record>("users") {
        companion object {
            val USER = User()
        }
        val id: Field<Int> = createField("id", SQLDataType.INTEGER)
        val name: Field<String> = createField("name", SQLDataType.NVARCHAR(50))
        val countryId: Field<Short> = createField("country_id", SQLDataType.SMALLINT)
    }
    

    但是,对于您可能无法获得的各种高级 jOOQ 功能,强烈建议使用代码生成器。

    【讨论】:

    • 谢谢 Lukas 另外我可以在免费计划中使用 mssql 的代码生成器功能吗?
    • @MuratSeyis:jOOQ 专业版提供 mssql 支持,请参阅 jooq.org/legal/licensing#databases
    猜你喜欢
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多