kotlin - 带有 Jetbrains 的 Exposed 库的字符串主键 - Kotlin
问题描述
我在弄清楚如何使用字符串主键编写表并拥有表的实体时遇到问题。无论我IdTable<String>
是作为 Table 类型还是尝试将它与普通的类型一起使用Table
都不起作用。
解决方案
如果您确实需要将 String 作为主键,请执行以下操作:
/*
* Base class for entities with string id
*/
abstract class StringEntityClass<out E: Entity<String>>(table: IdTable<String>, entityType: Class<E>? = null) : EntityClass<String, E>(table, entityType)
/*
* Base class for table objects with string id
*/
open class StringIdTable(name: String = "", columnName: String = "id", columnLength: Int = 10) : IdTable<String>(name) {
override val id: Column<EntityID<String>> = varchar(columnName, columnLength).entityId()
override val primaryKey by lazy { super.primaryKey ?: PrimaryKey(id) }
}
// Sample usage
object MyTableWithStringId : StringIdTable() {
// ...
}
class MyEntityWithStringId(id: EntityID<String>) : Entity<String>(id) {
// ...
}
推荐阅读
- ansible-tower - 任务结果到文件
- java - dynamodb 增强:如何在 java(spring-boot) 中使用 SecondaryIndex 进行查询
- java - 在 Ubuntu 20.04 java 上没有播放:为每个可能的混音器抛出 LineUnavailableException
- python - 尝试在 Python 中创建人工数据框时遇到错误
- python - 我应该把我的暂停键放在哪里?
- reactjs - 反应导航路由器 v6 无效的钩子调用
- mysql - 为什么mysql中的存储过程使用laravel返回没有OUT参数的数据
- go - 如何为枚举类型分配默认值
- debugging - 是否可以同时使用 gdb 和嵌套的 qemu 虚拟机来调试 linux 用户空间程序和主机管理程序?
- c# - 复制或移动替换文件和更新版本共享点 CSOM