Skip to content

Commit

Permalink
fix: EXPOSED-621 IllegalStateException on accessing autoincrement col…
Browse files Browse the repository at this point in the history
…umn after insert using Entity

The issue was that the `hashCode()` function was using `sequence` which invokes `autoincSeq` if `_sequence` is null, which then invokes `currentDialect`, and if this is not within a transaction, it will throw an error. The fix is to use `_sequence` instead of `sequence` when generating a hashcode.
  • Loading branch information
joc-a committed Nov 1, 2024
1 parent 64bcd69 commit be851b6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ class AutoIncColumnType<T>(
var result = delegate.hashCode()
result = 31 * result + (_autoincSeq?.hashCode() ?: 0)
result = 31 * result + fallbackSeqName.hashCode()
result = 31 * result + (sequence?.hashCode() ?: 0)
result = 31 * result + (_sequence?.hashCode() ?: 0)
return result
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.jetbrains.exposed.sql.tests.shared.ddl

import org.jetbrains.exposed.dao.UUIDEntity
import org.jetbrains.exposed.dao.UUIDEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.dao.id.UUIDTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.tests.DatabaseTestsBase
import org.jetbrains.exposed.sql.tests.TestDB
Expand All @@ -11,8 +14,12 @@ import org.jetbrains.exposed.sql.tests.inProperCase
import org.jetbrains.exposed.sql.tests.shared.assertEquals
import org.jetbrains.exposed.sql.tests.shared.assertFalse
import org.jetbrains.exposed.sql.tests.shared.assertTrue
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.vendors.currentDialect
import org.junit.Assume
import org.junit.Test
import java.util.*
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull

Expand Down Expand Up @@ -311,6 +318,48 @@ class SequencesTests : DatabaseTestsBase() {
}
}

@Test
fun testAutoIncrementColumnAccessWithEntity() {
Assume.assumeTrue(TestDB.POSTGRESQL in TestDB.enabledDialects())

Database.connect(
TestDB.POSTGRESQL.connection(),
TestDB.POSTGRESQL.driver,
TestDB.POSTGRESQL.user,
TestDB.POSTGRESQL.pass
)

try {
transaction {
SchemaUtils.create(TesterTable)
}

val testerEntity = transaction {
TesterEntity.new {
name = "test row"
}
}

assertEquals(1, testerEntity.index)
} finally {
transaction {
SchemaUtils.drop(TesterTable)
}
}
}

object TesterTable : UUIDTable("Tester") {
val index = integer("index").autoIncrement()
val name = text("name")
}

class TesterEntity(id: EntityID<UUID>) : UUIDEntity(id) {
companion object : UUIDEntityClass<TesterEntity>(TesterTable)

var index by TesterTable.index
var name by TesterTable.name
}

private object Developer : Table() {
val id = integer("id")
val name = varchar("name", 25)
Expand Down

0 comments on commit be851b6

Please sign in to comment.