Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityClass.new(id) with EntityClass.get throws NoSuchElementException #731

Closed
t-kameyama opened this issue Dec 18, 2019 · 1 comment · Fixed by #733
Closed

EntityClass.new(id) with EntityClass.get throws NoSuchElementException #731

t-kameyama opened this issue Dec 18, 2019 · 1 comment · Fixed by #733

Comments

@t-kameyama
Copy link
Contributor

Reproduced in 0.19.3 (Not reproduced in 0.19.1)

Test.kt

import org.jetbrains.exposed.dao.LongEntity
import org.jetbrains.exposed.dao.LongEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction

object ParentTable : LongIdTable() {
    val name = varchar("name", 50)
}
class ParentEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<ParentEntity>(ParentTable)
    var name by ParentTable.name
}

object ChildTable : LongIdTable() {
    val companyId = reference("company_id", ParentTable)
    val name = varchar("name", 80)
}
class ChildEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<ChildEntity>(ChildTable)
    var parent by ParentEntity referencedOn ChildTable.companyId
    var name by ChildTable.name
}

fun main() {
    Database.connect("jdbc:postgresql://localhost:5432/xxx", driver = "org.postgresql.Driver", user = "xxx", password = "")

    try {
        val parentId = transaction {
            SchemaUtils.create(ParentTable, ChildTable)
            ParentEntity.new {
                name = "parent1"
            }.id.value
        }

        transaction {
            ChildEntity.new(100L) {
                parent = ParentEntity[parentId]
                name = "child1"
            }
        }

    } finally {
        transaction {
            SchemaUtils.drop(ParentTable, ChildTable)
        }
    }
}
Exception in thread "main" java.util.NoSuchElementException: List is empty.
	at kotlin.collections.CollectionsKt___CollectionsKt.last(_Collections.kt:362)
	at org.jetbrains.exposed.sql.statements.BatchInsertStatement.allColumnsInDataSet(BatchInsertStatement.kt:59)
	at org.jetbrains.exposed.sql.statements.BatchInsertStatement.getArguments(BatchInsertStatement.kt:63)
	at org.jetbrains.exposed.sql.statements.InsertStatement.arguments(InsertStatement.kt:147)
	at org.jetbrains.exposed.sql.QueriesKt.batchInsert(Queries.kt:128)
	at org.jetbrains.exposed.sql.QueriesKt.batchInsert$default(Queries.kt:89)
	at org.jetbrains.exposed.dao.EntityCache.flushInserts$exposed_dao(EntityCache.kt:113)
	at org.jetbrains.exposed.dao.EntityCache.flush(EntityCache.kt:81)
	at org.jetbrains.exposed.dao.EntityLifecycleInterceptor.flushEntities(EntityLifecycleInterceptor.kt:56)
	at org.jetbrains.exposed.dao.EntityLifecycleInterceptor.beforeExecution(EntityLifecycleInterceptor.kt:13)
	at org.jetbrains.exposed.sql.statements.Statement.executeIn$exposed_core(Statement.kt:36)
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:125)
	at org.jetbrains.exposed.sql.Transaction.exec(Transaction.kt:111)
	at org.jetbrains.exposed.sql.Query.iterator(Query.kt:212)
	at org.jetbrains.exposed.sql.IterableExKt$mapLazy$1.iterator(IterableEx.kt:124)
	at kotlin.collections.CollectionsKt___CollectionsKt.firstOrNull(_Collections.kt:221)
	at org.jetbrains.exposed.dao.EntityClass.findById(EntityClass.kt:41)
	at org.jetbrains.exposed.dao.EntityClass.get(EntityClass.kt:19)
	at org.jetbrains.exposed.dao.EntityClass.get(EntityClass.kt:21)
	at TestKt$main$1$1.invoke(Test.kt:40)
	at TestKt$main$1$1.invoke(Test.kt)
	at org.jetbrains.exposed.dao.EntityClass.new(EntityClass.kt:242)
	at TestKt$main$1.invoke(Test.kt:39)
	at TestKt$main$1.invoke(Test.kt)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$1.invoke(ThreadLocalTransactionManager.kt:156)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$inTopLevelTransaction$2.invoke(ThreadLocalTransactionManager.kt:197)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.inTopLevelTransaction(ThreadLocalTransactionManager.kt:196)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt$transaction$1.invoke(ThreadLocalTransactionManager.kt:134)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.keepAndRestoreTransactionRefAfterRun(ThreadLocalTransactionManager.kt:205)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:106)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction(ThreadLocalTransactionManager.kt:104)
	at org.jetbrains.exposed.sql.transactions.ThreadLocalTransactionManagerKt.transaction$default(ThreadLocalTransactionManager.kt:103)
	at TestKt.main(Test.kt:38)
	at TestKt.main(Test.kt)

Process finished with exit code 1
@Tapac
Copy link
Contributor

Tapac commented Dec 24, 2019

@t-kameyama , thank you for a fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants