Skip to content

save() method on repository not working correctly #738

@kylooh

Description

@kylooh

Device: MacOS with M1 chip
Language: Kotlin
Spring R2dbc Version: spring-boot-starter-data-r2dbc:2.6.3
Driver Version: r2dbc-mysql:0.8.2.REALEASE

Here is my codes.

@Repository
interface ProductR2Repository : CoroutineCrudRepository<ProductEntity, String> {

}
@Table(value = "product")
data class ProductEntity(
    @Id val code: String,
    val name: String,
    val expectedReturn: Double,
    val expectedRisk: Double,
    val creator: String,
    val createTime: String,
    val updateTime: String
)

It throws Exception in thread "main" ApplicationErrorException (0x201): Failed to update table [product]. Row with Id [testing-0] does not exist.

And I found the problem is, save() method will pre-check entity's isNew() status, then insert() if new, update() if not. And PersistentEntity's isNew() method doing following behaviors.

  1. find entity's id field which annotation by @Id. If id is null, returns true. Which means entity without @Id is always new.
  2. check id is null or a primitive type. If it is not null or primitive, returns false, which means using update. Which means entity without any id field is always new.
  3. if id is not null, and also a number value, and also equals 0, returning true. Which means entity id equals 0 is always new.

I used JPA before, so I'm hardly understand why exists check using that way, instead of do check exists like using existsById() method. And also I removed @Id from entity, but it will throws exception

Exception in thread "main" ApplicationErrorException (0x201): Required identifier property not found for class com.betalpha.w4.infra.r2dbc.product.info.simulate.ProductEntity!

And If I using null as id's value, it throws

nested exception is io.r2dbc.spi.R2dbcDataIntegrityViolationException: [23502] [23502] NULL not allowed for column "CODE";

Anyone has any idea for this problems? Help, please

Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions