spring-projects / spring-data-r2dbc

Provide support to increase developer productivity in Java when using Reactive Relational Database Connectivity. Uses familiar Spring concepts such as a DatabaseClient for core API usage and lightweight repository style data access.
Apache License 2.0
708 stars 132 forks source link

Repository's save method throws class java.lang.Long cannot be cast to class java.lang.Integer exception #812

Closed vmonastyrov closed 1 year ago

vmonastyrov commented 1 year ago

Language: Kotlin Spring R2dbc Version: spring-boot-starter-data-r2dbc:2.7.8 Driver Version: r2dbc-h2:1.0.0.RELEASE

My code:

interface ProducerRepository : CoroutineCrudRepository<cProducerEntity, Long> {}
@Table(name = TABLE_NAME)
data class ProducerEntity(
  @Id
  @Column("${FIELD_PREFIX}_id")
  val id: Long,

  @Column("${FIELD_PREFIX}_name")
  val name: String?,

  @Column("${FIELD_PREFIX}_description")
  val description: String?,

  @Column("${FIELD_PREFIX}_registered")
  val registered: Boolean,

  @Column("${FIELD_PREFIX}_writer_id")
  val writerId: Long,

  @Column("${FIELD_PREFIX}_url")
  val url: String,

  @Column("${FIELD_PREFIX}_created_at")
  val createdAt: LocalDateTime,

  @Column("${FIELD_PREFIX}_updated_at")
  @LastModifiedDate
  val updatedAt: LocalDateTime
)
suspend fun unregisterProducer(id: Long): Result<ProducerEntity> {
    val producer = repo.findById(id) ?: return Result.failure(Throwable("Producer can not be null"))
    return writerClient.unregisterProducer(producer.writerId).map { 
      repo.save(producer.copy(registered = false, writerId = 0L))
    }
  }

It throws Exception: java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap') java.lang.ClassCastException: class java.lang.Long cannot be cast to class java.lang.Integer (java.lang.Long and java.lang.Integer are in module java.base of loader 'bootstrap') at java.base/java.util.stream.Collectors.lambda$summingInt$19(Collectors.java:673) Despite the exception row in DB is updated.

mp911de commented 1 year ago

Spring Boot 2.7 supports only R2DBC 0.9. Please either downgrade your driver or upgrade to Spring Boot 3.0.

vmonastyrov commented 1 year ago

Thank you a lot for your answer. Unfortunately I cannot upgrade to Spring Boot 3.0 therefore I downgraded h2 driver and the save method works now.