https://kotlinlang.org logo
#exposed
Title
# exposed
r

Ron S

04/27/2022, 8:45 AM
Copy code
class UserEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<UserEntity>(UserTable)
    var username by UserTable.username
}

class TestEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<TestEntity>(TestTable)
    var author by UserEntity referencedOn TestTable.autor
//    var author by TestTable.author // not this way
}

transaction {
    TestEntity.all().first().author // triggers fetch of entire entity
    TestEntity.all().first().readValues[TestTable.author] // returns the desired id but is a little awkward
}
I want to benefit from the dao api by fetching entire related entites (like author here). However, there are some use cases when the id of the referenced entity is sufficient and I don't want to cause another database query to fetch the other unneeded columns. The only - but awkward - way I found to achieve this was by using "readValues". Is there any better solution that I have overlooked?
1
h

hfhbd

04/27/2022, 12:36 PM
What is wrong with
var authorID by TestTable.author // or better val
? This works if
authorID
is a FK.
r

Ron S

04/27/2022, 1:08 PM
Then I lost the ability to fetch the entire UserEntity (in case I need more than just the id). That way I can solely access the value of the foreign key.
h

hfhbd

04/27/2022, 1:08 PM
You could simple add both properties
r

Ron S

04/27/2022, 1:10 PM
I'm quite sure I tried. It must have said smth like "duplicate column names for 'author'"
h

hfhbd

04/27/2022, 1:12 PM
This sounds strange. We use this syntax in almost every entity class:
Copy code
class TestEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<TestEntity>(TestTable)

    var author by UserEntity referencedOn TestTable.autor
    var authorID by TestTable.author
}
r

Ron S

04/27/2022, 1:14 PM
Okay, Im gonna try this out again straight away. Maybe I did a careless mistake.
Well... it works. For some reason I assumed that it doesn't work. Thank you, sometimes you just need another person that can look at the situation without bias.
h

hfhbd

04/27/2022, 1:26 PM
Yeah, or a duck 😄
r

Ron S

04/27/2022, 1:27 PM
That's hilarious, I will definitely get myself one.
14 Views