-
Notifications
You must be signed in to change notification settings - Fork 131
Description
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.
- find entity's
idfield which annotation by@Id. If id is null, returns true. Which means entity without@Idis always new. - check
idis 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. - 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