Ron S

    Ron S

    4 months ago
    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?
    hfhbd

    hfhbd

    4 months ago
    What is wrong with
    var authorID by TestTable.author // or better val
    ? This works if
    authorID
    is a FK.
    Ron S

    Ron S

    4 months ago
    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.
    hfhbd

    hfhbd

    4 months ago
    You could simple add both properties
    Ron S

    Ron S

    4 months ago
    I'm quite sure I tried. It must have said smth like "duplicate column names for 'author'"
    hfhbd

    hfhbd

    4 months ago
    This sounds strange. We use this syntax in almost every entity class:
    class TestEntity(id: EntityID<Long>) : LongEntity(id) {
        companion object : LongEntityClass<TestEntity>(TestTable)
    
        var author by UserEntity referencedOn TestTable.autor
        var authorID by TestTable.author
    }
    Ron S

    Ron S

    4 months ago
    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.
    hfhbd

    hfhbd

    4 months ago
    Yeah, or a duck 😄
    Ron S

    Ron S

    4 months ago
    That's hilarious, I will definitely get myself one.