Skip to content

Fix sample transaction code. was: Difference save entity between using repository and transaction #1524

Closed
@rbleuse

Description

@rbleuse

Hello,

I was playing with transactions provided by couchbase and spring data following the documentation from here
I noticed that there's a difference if I persist a document with a repository or with a transaction.

Here are details :
spring-boot 2.7.2
couchbase-transactions 1.2.4

Simple entity

@Document
@Scope("dev")
@Collection("schedule")
data class Schedule(
    @field:Id
    val id: String,

    val nested: Nested,

    @field:Field
    val list: List<String>
)

data class Nested(
    @field:Field("isFree")
    val free: Boolean
)

Repository

interface ScheduleRepository : ReactiveCouchbaseRepository<Schedule, String>

Service

private val collection = clientFactory.withScope("dev").getCollection("schedule").reactive()

fun saveScheduleWithRepo(schedule: Schedule) = repository.save(schedule)

fun saveScheduleAsDocWithTransaction(schedule: Schedule): Mono<TransactionResult> {
    val target = CouchbaseDocument()
    converter.write(schedule, target)
    return transactions.reactive().run {
        it.insert(collection, target.id, target.content)
            .then()
    }
}
val schedule = Schedule("1", Nested(true), listOf("TEST"))
return service.saveScheduleWithRepo(schedule)
    .flatMap {
        service.saveScheduleAsDocWithTransaction(schedule.copy(id = "2"))
    }

Here are differences :

with repository as doc with transaction
{
  "_class": "com.rbleuse.spring.reactive.couchbase.model.Schedule",
  "list": [
    "TEST"
  ],
  "nested": {
    "isFree": true
  }
}
{
  "_class": "com.rbleuse.spring.reactive.couchbase.model.Schedule",
  "list": {
    "empty": false
  },
  "nested": {
    "content": {
      "isFree": true
    },
    "id": null,
    "expiration": 0
  }
}

As you can see, inserting the document with a transaction adds extra fields to the document : content, id and expiration (all in nested). Shouldn't it be in root object instead ?
Furthermore, my simple list of string is not converted into an array with the actual String value.
If I convert my List into an Array in my data class, result is same when using a transaction.

Did I miss something ?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions