Skip to content

in Kotlin, entity with identifier column of type Int does not insert this column when column has value 0 #1968

@jzelenka

Description

@jzelenka

Kotlin 2.1.0
spring-boot-starter-data-jdbc 3.4.0

import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table

@Table("TEST_SIMPLE_TABLE")
data class SimpleTable(
    @Id
    var simpleId: Int,
    var name: String
) 

val simpleTable = SimpleTable(simpleId = 0, name = "something")
jdbcAggregateTemplate.insert(entity)

this generates SQL without SIMPLE_ID column and obviously fails:

INSERT INTO "TEST_SIMPLE_TABLE" ("NAME") VALUES ('something')

java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("TEST_SIMPLE_TABLE"."SIMPLE_ID")

it is related with Kotlin's Int bytecode mapping (Int -> int, Int? -> java.lang.Integer)

  • when simpleId is set to other value than 0, it works
  • when simpleId is defined as Int? , it works

so Int? is workaround, but not ideal to loose nullability check.
Looks like bug to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: invalidAn issue that we don't feel is valid

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions