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.

Add the ability to override how enums are mapped to the database.

See original GitHub issue

Use case

I would like to able to map enums in Kotlin to integers in the database. The pre-existing databases that I am developing against (in SQL Server) have all the enum-like columns stored as integers often with a backing constraint table.

Describe the solution you’d like

I would like to be able to provide a custom DataType converter to map the enums but it does not work. The komapper-processor is currently hard-coded to prioitise mapping enums to strings.

https://github.com/komapper/komapper/blob/fdf89c272c272fa5b56ee8e18e1afb326b5ae4ee/komapper-processor/src/main/kotlin/org/komapper/processor/EntityFactory.kt#L62

Versions

  • Komapper: 1.0.0-RC1
  • Kotlin: 1.6.21
  • JVM: JDK 14.0.1
  • JDBC Driver or R2DBC Driver: com.microsoft.sqlserver:mssql-jdbc:10.2.0.jre11
  • Database: SQL Server 2014
  • OS: Windows 10

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
donard-commedaghcommented, May 23, 2022

That’s a good idea, I’ve only just read up on value classes in the last week and haven’t used them anywhere yet. As a temporary fix, I can live without the exhaustive when(enum) and enum.values()

Thank you for the excellent work developing Komapper.

0reactions
nakamura-tocommented, May 23, 2022

By the way, you can use value class instead of enum class. For example, the following code works with Komapper 1.0.0-RC1:

@JvmInline
value class Color(val value: Int) {
    companion object {
        val BLUE = Color(0)
        val RED = Color(1)
        val YELLOW = Color(2)
    }
}

@KomapperEntity
data class Pencil(
    @KomapperId
    val id: Int,
    val color: Color
)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate Tips: How to map an Enum to a database column
In this Hibernate Tip, I show you how to map an Enum to a database column. You can choose between 2 standard options...
Read more >
Mapping enums done right with @Convert in JPA 2.1
@Enumerated(EnumType.ORDINAL) (default) will map enum values using Enum.ordinal() . Basically first enumerated value will be mapped to 0 ...
Read more >
Persisting Enums in JPA - Baeldung
A quick and practical guide to persisting enums in JPA. ... there's no convenient way to map Enum values to a database column....
Read more >
Map enum in JPA with fixed values? - Stack Overflow
A simple solution is to use the Enumerated annotation with EnumType.ORDINAL: @Column(name = "RIGHT") @Enumerated(EnumType.ORDINAL) private Right right;.
Read more >
Mapping Enums Done Right With @Convert in JPA 2.1 - DZone
@Enumerated(EnumType.STRING) is much safer because it stores string representation of enum . You can now safely add new values and move them ...
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