Skip to content

Commit

Permalink
NoSuchElementException: List is empty on updating not flushed entities
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Dec 10, 2019
1 parent f33807a commit f7dd832
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ open class UserDataHolder {

open class Transaction(private val transactionImpl: TransactionInterface): UserDataHolder(), TransactionInterface by transactionImpl {

init {
Companion.globalInterceptors // init interceptors
}
internal val interceptors = arrayListOf<StatementInterceptor>()

fun registerInterceptor(interceptor: StatementInterceptor) = interceptors.add(interceptor)
Expand Down
12 changes: 10 additions & 2 deletions exposed-dao/src/main/kotlin/org/jetbrains/exposed/dao/Entity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.exposed.dao

import org.jetbrains.exposed.dao.exceptions.EntityNotFoundException
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.TransactionManager
Expand Down Expand Up @@ -33,11 +34,18 @@ open class Entity<ID:Comparable<ID>>(val id: EntityID<ID>) {
* @throws EntityNotFoundException if entity no longer exists in database
*/
open fun refresh(flush: Boolean = false) {
if (flush) flush() else writeValues.clear()
val cache = TransactionManager.current().entityCache
val isNewEntity = id._value == null
when {
isNewEntity && flush -> cache.flushInserts(klass.table)
flush -> flush()
isNewEntity -> throw EntityNotFoundException(this.id, this.klass)
else -> writeValues.clear()
}

klass.removeFromCache(this)
val reloaded = klass[id]
TransactionManager.current().entityCache.store(this)
cache.store(this)
_readValues = reloaded.readValues
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ abstract class EntityClass<ID : Comparable<ID>, out T: Entity<ID>>(val table: Id
* @param flush whether pending entity changes should be flushed previously
*/
fun reload(entity: Entity<ID>, flush: Boolean = false): T? {
if (flush) entity.flush()
if (flush) {
if (entity.id._value == null)
TransactionManager.current().entityCache.flushInserts(table)
else
entity.flush()
}
removeFromCache(entity)
return findById(entity.id)
}
Expand Down

0 comments on commit f7dd832

Please sign in to comment.