gpt4 book ai didi

Kotlin Excessed 通过引用创建实体

转载 作者:行者123 更新时间:2023-12-02 02:23:09 26 4
gpt4 key购买 nike

我正在尝试使用 Kotlin 上的 Exposed 编写 CRUD 服务。我有一张包含多对一引用的表格。当我尝试插入时,我得到

java.lang.IllegalStateException: No transaction in context.

这是表和实体

object Contacts : IntIdTable("ph_contact"){
var firstName = varchar("first_name",255)
var lastName = varchar("last_name",255)
var phone = varchar("phone",4096)
var email = varchar("email",4096)
var user = reference("user",Users)
}

class ContactEntity (id: EntityID<Int>): IntEntity(id){
companion object : IntEntityClass<ContactEntity>(Contacts)

var firstName by Contacts.firstName
var lastName by Contacts.lastName
var phone by Contacts.phone
var email by Contacts.email
var user by UserEntity referencedOn Contacts.user


fun toContact() = Contact(id.value,user.id.value,firstName,lastName,phone,email)

}


data class Contact(
val id: Int,
val userId: Int,
val firstName: String,
val lastName: String,
val phone: String,
val email: String
)

这里是创建服务方法

fun createContact(contact: Contact) = transaction {

ContactEntity.new {

this.firstName=contact.firstName
this.lastName=contact.lastName
this.phone=contact.phone
this.email=contact.email
this.user= UserEntity[contact.userId]

}
}

保存实体后调用toContact()方法时抛出错误

var contact = contactEntity.toContact()

创建此类实体的正确方法是什么?

最佳答案

您需要在 create 方法的事务中调用 toContact 方法。

记录的嵌套用户记录只能从事务内部延迟访问。

我通过让任何数据访问层接口(interface)使用我自己的数据类来解决这个问题。暴露的记录和表在该层之下保持私有(private)。不要让事务实体在其上方泄漏。

下面的示例使用嵌套的 User 来说明:

object Users : IntIdTable("ph_users") {
var otherUserData = varchar("other_user_data", 255)
}

class UserEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<UserEntity>(Users)

var otherUserData by Users.otherUserData


fun toUser() = User(id.value, otherUserData)

}

object Contacts : IntIdTable("ph_contact") {
var firstName = varchar("first_name", 255)
var lastName = varchar("last_name", 255)
var phone = varchar("phone", 4096)
var email = varchar("email", 4096)
var user = reference("user", Users)
}

class ContactEntity(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ContactEntity>(Contacts)

var firstName by Contacts.firstName
var lastName by Contacts.lastName
var phone by Contacts.phone
var email by Contacts.email
var user by UserEntity referencedOn Contacts.user

fun toContact() = Contact(id.value, user.toUser(), firstName, lastName, phone, email)
}


data class Contact(
val id: Int,
val user: User,
val firstName: String,
val lastName: String,
val phone: String,
val email: String
)

data class User(
val id: Int,
val otherUserData: String
)

class ContactsRepo {

fun createContact(contact: Contact): Contact = transaction {
ContactEntity.new {
this.firstName = contact.firstName
this.lastName = contact.lastName
this.phone = contact.phone
this.email = contact.email
this.user = UserEntity[contact.user.id]

}.toContact()
}

}

编辑:另一种方法是不让用户记录引用,而只是将其保留为用户 ID?但您可能在其他查询中需要此嵌套记录。

关于Kotlin Excessed 通过引用创建实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66109368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com