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

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

Closed
jzelenka opened this issue Jan 4, 2025 · 3 comments
Labels
status: invalid An issue that we don't feel is valid

Comments

@jzelenka
Copy link

jzelenka commented Jan 4, 2025

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 4, 2025
@schauder
Copy link
Contributor

schauder commented Jan 6, 2025

I don't see why this should be a bug. It is clearly documented that an entity with id field of type int with value 0 is considered new and the id is expected to be generated by the database by default.

If you want the id to be generated in a different way, you may resort to an EntityCallBack

If you want to change the Entity State detection, the first link reference gives you some options.

If you want to just force an insert you may use JdbcAggregateTemplate#insert

Finally this blog article might be of some use: https://spring.io/blog/2021/09/09/spring-data-jdbc-how-to-use-custom-id-generation

@schauder schauder closed this as not planned Won't fix, can't repro, duplicate, stale Jan 6, 2025
@schauder schauder added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 6, 2025
@jzelenka
Copy link
Author

jzelenka commented Jan 6, 2025

You are correct, just one clarification - JdbcAggregateTemplate#insert does not help, it is actually what I tried and described above. So entity change detection also does not help, because I am already doing insert.
Thanks for the info anyway :)

@schauder
Copy link
Contributor

schauder commented Jan 6, 2025

I agree.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants