spring-projects / spring-projects/spring-data-relational

NPE in BasicRelationalConverter: @WritingConverter in combination with null [DATAJDBC-578]

Open
#798 1 comment 0 reactions 1 assignee View on GitHub

@schauder is already working on this.

Since Dec 31, 2020.

type: enhancement
Dominant language
Java
Stars
827
Forks
394
PR merge metrics
No merged PRs in 30d

Description

Clemens Hahn opened DATAJDBC-578 and commented

I have a Entity Ticket with a column status which is a enum:

data class Ticket(
        @Id var id: Long? = null,
        val title: String,
        val assigned: String? = null,
        val status: TicketStatus = TicketStatus.UNKNOWN
) {
    enum class TicketStatus(private val databaseValue: String?) {
        OPEN("oPeN"),
        SOLVED("solVed"),
        UNKNOWN(null);

        @ReadingConverter
        class EnumReadingConverter : Converter<String, TicketStatus> {
            override fun convert(dbValue: String): TicketStatus {
                return values().first { it.databaseValue == dbValue }
            }
        }

        @WritingConverter
        class EnumWritingConverter : Converter<TicketStatus, String> {
            override fun convert(entity: TicketStatus): String? {
                return entity.databaseValue
            }
        }
    }
}

As you can see the TicketStatus UNKNOWN should be represented by null in the database.

Perhaps this is a bad approach at all, but I noticed a hard NullPointerException in org.springframework.data.relational.core.conversion.BasicRelationalConverter.getPotentiallyConvertedSimpleWrite(BasicRelationalConverter.java:214). Full stacktrace is attached.

In order to reproduce this case please see my created spring-boot project at GitHub (ticket/DATAJDBC-578 branch), especially the tests in com.example.springdatajdbc.showcase.ShowcaseApplicationTests

So the root cause of this exception is not spring-data-jdbc. Please change the jira-project if necessary, thanks!


Attachments:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.