我正在使用Spring Data Neo4j(5.1.8.RELEASE)。
假设我有一个包含简单关系的简单节点。
如果我创建一个与entity2有关系的简单entity1,然后仅更新entity1,则我希望仅entity1版本字段会增加。令人惊讶的是,关系版本似乎也增加了。这是设计使然,还是一个错误?
@NodeEntity
class TestEntity(
var content: String,
@Relationship(direction = OUTGOING)
var relationships: MutableList<TestRelationship>? = null
) : AbstractEntity() {
fun addRelationship(testEntity: TestEntity) {
if (relationships == null) {
relationships = mutableListOf(TestRelationship(this, testEntity))
} else {
relationships?.add(TestRelationship(this, testEntity))
}
}
}
@RelationshipEntity
class TestRelationship(
@StartNode
var startNode: TestEntity,
@EndNode
var endNode: TestEntity
) : AbstractEntity()
abstract class AbstractEntity {
@Id
@GeneratedValue(strategy = UuidStrategy::class)
var id: String? = null
@Version
var version: Long? = null
@LastModifiedBy
var updatedBy: String? = null
@LastModifiedDate
var updatedTime: Instant? = null
}
@Test
@DisplayName("Test entity version")
fun testEntityVersion() {
// Given
val entity1 = TestEntity("Test1")
val entity2 = TestEntity("Test2")
entity1.addRelationship(entity2)
repository.save(entity1)
// When
entity1.content = "Test1 (updated)"
repository.save(entity1)
// Then
Assertions.assertThat(entity1.version).isEqualTo(1) // ok
Assertions.assertThat(entity1.relationships!![0].version).isEqualTo(0) // fails!! expecting 0 but actual value is 1
Assertions.assertThat(entity2.version).isEqualTo(0) // ok
}