question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I have the following code.

class Author(id: EntityID<Int>): IntEntity(id){
    companion object : IntEntityClass<Author>(Authors)

    var firstName by Authors.firstName
    var lastName by Authors.lastName
    override fun toString(): String {
        return "Author($id, $firstName, $lastName)"
    }
}
object Authors: IntIdTable(){
    val firstName = varchar("firstName", 50)
    val lastName = varchar("lastName", 50)
}
class Book(id: EntityID<Int>): IntEntity(id){
    companion object : IntEntityClass<Book>(Books)

    var name by Books.name
    var authors by Author via BookAuthors
    override fun toString(): String {
        return "Book($id, $name, authors: [$authors])"
    }
}
object Books: IntIdTable(){
    val name = varchar("name", 50)
}
object BookAuthors: Table() {
    val book = reference("book", Books).primaryKey(0)
    val author = reference("author", Authors).primaryKey(1)
}

Studing documentation I can add new Book and Author

val book = transaction {
	Book.new {
		name = "test book"
	}
}
val author = transaction {
	 Author.new {
		firstName = "FName"
		lastName = "LName"
	}
}

as well as create reference

transaction {
	 book.authors = SizedCollection(listOf(author))
}

The problem I have is that after getting all books with eager loading, collection is not printed as expected

for (bookItem in Book.all().with(Book::authors)) {
	println(bookItem)
}

Reult Book(1, test book, authors: [org.jetbrains.exposed.sql.LazySizedCollection@7c56e013])

val book1 = Book.findById(1)?.load(Book::authors)
println(book1)
//same result 
//Book(1, test book, authors: [org.jetbrains.exposed.sql.LazySizedCollection@7c56e013])

println(book1?.authors)
//authors not printed 
//org.jetbrains.exposed.sql.LazySizedCollection@7c56e013

println(book1?.authors?.with(Book::authors))
//now it can print authors? 
//[Author(1, FName, LName)]

println(book1)
//still same issue 
//Book(1, test book, authors: [org.jetbrains.exposed.sql.LazySizedCollection@7c56e013])

Could you help with that? What might be the problem? My assumption is that Book.findById(1)?.load(Book::authors) is not loading, am I correct?

Console debugg logs shows that authors are selected, but they are not populated in Book.all() DEBUG Exposed - SELECT AUTHORS.ID, AUTHORS."firstName", AUTHORS."lastName", BOOKAUTHORS.AUTHOR, BOOKAUTHORS.BOOK FROM AUTHORS INNER JOIN BOOKAUTHORS ON BOOKAUTHORS.AUTHOR = AUTHORS.ID WHERE BOOKAUTHORS.BOOK IN (1, 2)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

11reactions
uuf6429commented, May 29, 2020

All transformations that use the eager loaded relations have to be done inside the transaction closure, as relations are stored inside the cache for that specific transaction. This is just a limitation of the feature unfortunately. In short in your example above your println would need to go inside the transaction closure

@CharlieTap I have this same issue and unfortunately, it’s not clear why this problem happens. If the object graph is loaded eagerly in a transaction, why is another (or same) transaction needed again later on? Also, perhaps the point of eager loading is not very clear. cc @Tapac

2reactions
CharlieTapcommented, Oct 15, 2019

All transformations that use the eager loaded relations have to be done inside the transaction closure, as relations are stored inside the cache for that specific transaction. This is just a limitation of the feature unfortunately. In short in your example above your println would need to go inside the transaction closure

Read more comments on GitHub >

github_iconTop Results From Across the Web

language agnostic - What is eager loading? - Stack Overflow
Eager loading is also used in Angular 8. It just means that the instant the application is loaded inside the browser ...
Read more >
Eager Loading in Entity Framework
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query, so...
Read more >
What is Lazy Loading | Lazy vs. Eager Loading - Imperva
While lazy loading delays the initialization of a resource, eager loading initializes or loads a resource as soon as the code is executed....
Read more >
Eager Loading of Related Data - EF Core - Microsoft Learn
Eager loading a collection navigation in a single query may cause performance issues. For more information, see Single vs. split queries.
Read more >
Entity Framework - Eager Loading - Tutorialspoint
Eager loading is the process whereby a query for one type of entity also loads related entities as part of the query. Eager...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found