0
votes

I use Corda 4.1 and kotlin. My goal is to store some data to Corda databse using Persistence API and then do some queries(I follow the doc https://docs.corda.net/api-persistence.html ). I have small problem with querying enumerated types.

This enum

@CordaSerializable
enum class MyEnum {    
    A,
    B,
    C
}

is stored in the database table as integer values 0 1 2 And it looks like A corresponds 0, B corresponds 1, and C correponds 2.

But I do not see any explicit mappings. What rules are used to serialize enum values? can they be changed with some annotations?

Is there any simple way of ensuring that A enum value will be always saved as 0 in the database? Is it possible to persist just "A", "B", "C" directly instead of numbers?

I could do this maually implenmenting this logic in generateMappedObject function, but I am curious what is the right way

1

1 Answers

1
votes

See here in the Kotlin specification: https://kotlinlang.org/docs/reference/enum-classes.html#working-with-enum-constants

Ordinals are allocated by the order in which they are declared within the Enum, and you can access the ordinal programmatically by calling MyEnum.A.ordinal

It doesn't look like there is any easy way to explicitly allocate ordinals like in C# where you can say enum { A = 2, B = 1, C = 0 }

You can also tell Hibernate to persist the mapping as a string by defining your column in the mapped entity as:

    @Entity
    @Table(name = "table_containing_en_enum"
    class MyClass {

        @Column(name = "enum")
        @Enumerated(EnumType.STRING)
        val enum: MyEnum, 

        ...
    }