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

Accessing enum field in Entity created using EntityClass.new causes ClassCastException #464

Closed
carterhudson opened this issue Jan 9, 2019 · 3 comments
Assignees
Labels

Comments

@carterhudson
Copy link

carterhudson commented Jan 9, 2019

I've got the following:

object Cards : IntIdTable() {
    enum class Issuer {
        ISSUER,
        OTHER
    }

    val holder = reference("holder", Customers)
    val number = varchar("number", 255)
    val issuer = customEnumeration(
        "issuer",
        "ENUM('ISSUER', 'OTHER')",
        { Issuer.values()[it as Int] },
        { it.name }
    )
}
class CardEntity(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<CardEntity>(Cards)

    var number by Cards.number
    var customer by CustomerEntity referencedOn Cards.holder
    var issuer by Cards.issuer

    fun toDomain(): Card {
        return Card(
            id = id.value,
            number = number,
            issuer = issuer.name,
            holder = customer.id.value
        )
    }
}

When I do the following:

CardEntity
    .new {
        number = cardRequest.number
        customer = CustomerEntity[customerId]
        issuer = Cards.Issuer.valueOf(cardRequest.issuer)
    }
    .toDomain()

I get a ClassCastException stating data.card.Cards$Issuer cannot be cast to java.lang.Integer when the toDomain() function tries to read the issuer.name

The entry in the database gets created, and all subsequent fetches and uses of toDomain() work fine. It's seems that it's only when using the Entity returned by the .new { ... } block that this occurs. Any ideas?

Edit:
As a workaround, I can supply

{ value -> 
    (value as? Int)
        ?.let { Issuer.values()[it] } 
        ?: (value as Issuer) 
}

as my fromDb function to make things work, but that's pretty hacky...

@carterhudson carterhudson changed the title Accessing enum field in entity created using EntityClass.new causes ClassCastException Accessing enum field in Entity created using EntityClass.new causes ClassCastException Jan 9, 2019
@Tapac Tapac added the bug label Jan 10, 2019
Tapac added a commit that referenced this issue Jan 10, 2019
@Tapac Tapac self-assigned this Jan 10, 2019
@Tapac
Copy link
Contributor

Tapac commented Jan 10, 2019

Thank you for a report, will be available on the next release

@Tapac Tapac closed this as completed Jan 10, 2019
@hugo-clemente
Copy link

I think I'm having a problem related to this issue.

I'm using Exposed's DAO with the following :

class Joueur(id: EntityID<Int>) : IntEntity(id) {

    companion object : IntEntityClass<Joueur>(Joueurs)

    var positionJoueur by Joueurs.positionJoueur
    //Other properties...
}

object Joueurs : IntIdTable() {
    enum class PositionJoueur {
        GARDIEN,
        DEFENSEUR,
        MILIEU,
        ATTAQUANT,
        ENTRAINEUR
    }
    val positionJoueur = customEnumeration(
            "positionJoueur", 
            "ENUM('GARDIEN', 'DEFENSEUR', 'MILIEU', 'ATTAQUANT', 'ENTRAINEUR')",
            { value -> PositionJoueur.valueOf(value as String) }, { it.name })
    //Other properties...
}

When I'm creating such an entity, everything is fine, but when I'm trying to update it , I get the same ClassCastException stating java.lang.String cannot be cast to java.lang.Enum.

I'm using MySQL with Exposed 0.13.6

@Tapac
Copy link
Contributor

Tapac commented May 17, 2019

Could you share a reproducible example as I was trying to reproduce it without a success.

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

No branches or pull requests

3 participants