使用Kotlin和exposed,尝试使用链接表Map一对一关系

6xfqseft  于 2023-04-07  发布在  Kotlin
关注(0)|答案(1)|浏览(129)

我尝试使用链接表在两个实体之间建立一对一的关系。我有一个用户和角色实体,根据当前的要求,每个用户只有一个角色,尽管将来可能会改变。所以我使用链接表Map用户和角色关系。
到目前为止,我已经创建了用户表

object UserTable: LongIdTable("users", "internal_id") {
    val username = varchar("username", 100).uniqueIndex()
    val email = varchar("email", 100).uniqueIndex()
    val name = varchar("name", 100)
    val password = varchar("password", 250)
    val lastActivityTimestamp = datetime("last_activity_timestamp")
    val externalId = uuid("external_id").uniqueIndex()
    val createdBy = varchar("created_by", 100)
    val lastModifiedBy = varchar("last_modified_by", 100)
    val createdTimestamp = datetime("created_timestamp")
    val lastModifiedTimestamp = datetime("last_modified_timestamp").index()
    val status = varchar("status", 10).index()
}

以及角色表

object RoleTable: LongIdTable("roles", "internal_id") {
    val name = varchar("name", 100).uniqueIndex()
    val identifier = varchar("identifier", 100)
    val landingPageUri = varchar("landing_page_uri", 250)
    val mappedRolesJson = varchar("mapped_roles_json", 250)
    val externalId = uuid("external_id").uniqueIndex()
    val createdBy = varchar("created_by", 100)
    val lastModifiedBy = varchar("last_modified_by", 100)
    val createdTimestamp = datetime("created_timestamp")
    val lastModifiedTimestamp = datetime("last_modified_timestamp").index()
    val status = varchar("status", 10).index()
}

我还将链接表创建为

object UserAuthorityTable: Table("user_authorities") {
    val user = reference("user_id", UserTable)
    val role = reference("role_id", RoleTable)
    override val primaryKey = PrimaryKey(
        user, role
    )
}

我最终创建了角色实体

class Role(internalId: EntityID<Long>) : LongEntity(internalId) {
    companion object : LongEntityClass<Role>(RoleTable)
    var name by RoleTable.name
    var identifier by RoleTable.identifier
    var landingPageUri by RoleTable.landingPageUri
    var mappedRolesJson by RoleTable.mappedRolesJson
    var externalId by RoleTable.externalId
    var createdBy by RoleTable.createdBy
    var lastModifiedBy by RoleTable.lastModifiedBy
    var createdTimestamp by RoleTable.createdTimestamp
    var lastModifiedTimestamp by RoleTable.lastModifiedTimestamp
    var status by RoleTable.status
}

以及用户实体为

class User(internalId: EntityID<Long>) : LongEntity(internalId) {
    companion object : LongEntityClass<User>(UserTable)
    var username by UserTable.username
    var email by UserTable.email
    var name by UserTable.name
    var password by UserTable.password
    var role by Role via UserAuthorityTable
    var lastActivityTimestamp by UserTable.lastActivityTimestamp
    var externalId by UserTable.externalId
    var createdBy by UserTable.createdBy
    var lastModifiedBy by UserTable.lastModifiedBy
    var createdTimestamp by UserTable.createdTimestamp
    var lastModifiedTimestamp by UserTable.lastModifiedTimestamp
    var status by UserTable.status
}

但很明显,这表现为角色期望一个SizedIterable内部用户。我如何将链接表关系Map为“单数”?
我希望User实体通过链接表(而不是连接列)引用单个Role示例,并需要指导如何通过Exposed实现相同的功能

mrphzbgm

mrphzbgm1#

不要使用via,它代表多对多关系,你应该使用referencedOn

class User(internalId: EntityID<Long>) : LongEntity(internalId) {
    ...
    var role by Role referencedOn UserAuthorityTable.role
    ...
}

请注意,还需要指定用作引用的列。

相关问题