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.

[Postgres] enum ClassCastException

See original GitHub issue

Lib versions checked: 0.15.1, 0.16.2

Hi 😃 I’ve been looking through the existing (mostly closed) issues, but they seem old and only partially related.

Using the DAO API, and following examples in the docs, I’ve had enums working fine, until trying to set a column (creating and reading records has been ok).

Now, after setting a column value, the transaction flush throws an exception:

[…] PgEnum cannot be cast to java.lang.Enum

Hopefully I’m just doing something wrong, but it seems buried pretty deep in the DAO code. I can provide more of my code, and more stack trace if it helps 😃

My code in question looks like:

object ChallengeTable : IntIdTable() {
  val createdAt = datetime("createdAt")
  val slug = varchar("slug", 256).uniqueIndex()
  val status = ChallengeStatus.pgColumn(this, "status")
  val entryId = varchar("entryId", 64)
}

class ExposedChallenge(id: EntityID<Int>) : IntEntity(id) {
  companion object : IntEntityClass<ExposedChallenge>(ChallengeTable)

  var createdAt by ChallengeTable.createdAt
  var slug by ChallengeTable.slug
  var status by ChallengeTable.status
  var entryId by ChallengeTable.entryId
}

enum class ChallengeStatus {
  draft,
  live,
  completed,
  archive;

  companion object {
    const val dbName = "challenge_status"

    fun pgColumn(table: Table, name: String) = table.customEnumeration(
      name = name,
      sql = dbName,
      fromDb = { it.fromPg() },
      toDb = { it.toPg() }
    )

    private fun Any.fromPg() = valueOf(this as String)
    private fun ChallengeStatus?.toPg() = PgEnum(this)
  }

  class PgEnum(enumValue: ChallengeStatus?) : PGobject() {
    init {
      value = enumValue?.name
      type = dbName
    }
  }
}

And the call site (simplified):

transaction {
  ExposedChallenge
    .find { (ChallengeTable.slug eq "exampleSlug") }
    .first().apply {
      status = challengeStatus
    }
}

The first few stack slices:

java.lang.ClassCastException: ChallengeStatus$PgEnum cannot be cast to java.lang.Enum
        at org.jetbrains.exposed.sql.Table$customEnumeration$1.notNullValueToDB(Table.kt:235)
        at org.jetbrains.exposed.sql.IColumnType$DefaultImpls.nonNullValueToString(ColumnType.kt:51)
        at org.jetbrains.exposed.sql.ColumnType.nonNullValueToString(ColumnType.kt:60)
        at org.jetbrains.exposed.sql.IColumnType$DefaultImpls.valueToString(ColumnType.kt:43)
        at org.jetbrains.exposed.sql.ColumnType.valueToString(ColumnType.kt:60)
        at org.jetbrains.exposed.sql.QueryBuilder$registerArguments$1.invoke(Expression.kt:19)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
Tapaccommented, Sep 30, 2019

@abubics , thank you for your sample. It helped a lot and I was able to find the bug.

It will be fixed in upcoming 0.18.1.

1reaction
abubicscommented, Aug 5, 2019

I’ll try, but probably don’t have time until after the weekend… I’m moving house on Wednesday 😅 thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

JPA enum (java.lang.ClassCastException: org.postgresql.util ...
In JPA enums can be persisted as a text (name of the enum) or as a numerical value (ordinal of enum). @Enumerated(EnumType.
Read more >
JPA enum (java.lang.ClassCastException - postgresql
In JPA enums can be persisted as a text (name of the enum) or as a numerical value (ordinal of enum). @Enumerated(EnumType.STRING) tells...
Read more >
Formula of enum type results in ClassCastException
The build fails. The test failing is org.hibernate.test.annotations.enumerated.EnumeratedTypeTest > testTrimmedEnumChar FAILED The error is: org.h2.jdbc ...
Read more >
ClassCastException when using enum - Oracle Communities
Hi, I am using enum with @Enumerated annotation. I am using the enumType.STRING on my object and defined the DB column as string....
Read more >
getting H2/Hibernate to recognize the Postgresql "text" type as ...
getting H2/Hibernate to recognize the Postgresql "text" type as Java enum. 2347 views ... ClassCastException: java.io. ... nullSafeGet(EnumType.java:95)
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