I defined these simple models
@NodeEntity(label = "Api")
class Api {
@Id
@Property(name = "name")
var name: String = "name"
@Suppress("unused")
@Relationship(type = "EXPOSES_API", direction = Relationship.Direction.INCOMING)
val sandboxes: MutableList<Sandbox> = mutableListOf()
}
@NodeEntity(label = "Sandbox")
class Sandbox {
@Id
@Property(name = "name")
var name: String = "name"
@Relationship(type = "EXPOSES_API", direction = Relationship.Direction.OUTGOING)
val apis: MutableList<Api> = mutableListOf()
}
When I load all apis and modify the sandboxes in one api (remove one sandbox and another one) instance and save this instance , the removed sandbox still remain in the list on next load.
@Test
fun checkConsistency() {
val session = neoSession.sessionFactory.openSession()
session.clear()
val s1 = createSandbox("s1")
val s2 = createSandbox("s2")
val s3 = createSandbox("s3")
val a1 = createApi("a1", listOf(s1, s2))
val a2 = createApi("a2", listOf(s1, s2))
session.save(listOf(a1, a2))
var sandboxes = session.loadAll(Sandbox::class.java, 2)
assertThat(sandboxes.count()).isEqualTo(2)
var apis = session.loadAll(Api::class.java, 2)
assertThat(apis.size).isEqualTo(2)
assertThat(apis.fold(emptySet<Sandbox>()) { acc, a -> acc + a.sandboxes }.size).isEqualTo(2)
a2.sandboxes.clear()
a2.sandboxes.addAll(listOf(s1, s3))
// Here saving without depth introduce inconsistency
session.save(a2)
sandboxes = session.loadAll(Sandbox::class.java, 2)
apis = session.loadAll(Api::class.java, 2)
assertThat(sandboxes.count()).isEqualTo(3)
// The removed sandbox is still there
assertThat(apis.first { it.name == "a2" }?.sandboxes?.size).isEqualTo(3)
// So try again ...
a2.sandboxes.clear()
a2.sandboxes.addAll(listOf(s1, s3))
// Here saving with depth to remove inconsistency
session.save(a2, 2)
assertThat(apis.first { it.name == "a2" }?.sandboxes?.size).isEqualTo(2)
}
fun createApi(name: String, sandboxList: List<Sandbox>) = Api().apply {
this.name = name
sandboxes += sandboxList
}
fun createSandbox(name: String) = Sandbox().apply {
this.name = name
}
Full sample code available in my github repo here my reproducer
Thanks in advance
Patrice