我有一个结构,它(简化的)看起来像这样:
@NodeEntity(label = "Entity")
class FullEntity {
@Id @GeneratedValue
var _id: Long? = null
@Id @Index(unique = true)
lateinit var uuid: String
lateinit var someMoreData: String // this data is sometimes lost
@Relationship(type = "TARGETS", direction = Relationship.OUTGOING)
var target: StubEntity? = null
}
@NodeEntity(label = "Entity")
class StubEntity {
@Id @GeneratedValue
var _id: Long? = null
@Id @Index(unique = true)
lateinit var uuid: String
}
@RepositoryRestResource
interface EntityRepository : Neo4jRepository<FullEntity, Long>
现在,当我单独保存两个相关的FullEntity
对象时,如果我以一种方式保存,那么它都可以工作:
entityRepository.save(FullEntity().apply {
uuid = "uuid1"
someMoreData = "SomeMoreData1"
target = StubEntity().apply {
uuid = "uuid2"
}
})
// some time later ...
entityRepository.save(FullEntity().apply {
uuid = "uuid2"
someMoreData = "SomeMoreData2"
})
但是如果我像这样颠倒顺序:
entityRepository.save(FullEntity().apply {
uuid = "uuid2"
someMoreData = "SomeMoreData2"
})
// some time later ...
entityRepository.save(FullEntity().apply {
uuid = "uuid1"
someMoreData = "SomeMoreData1"
target = StubEntity().apply {
uuid = "uuid2"
}
})
它删除了"SomeMoreData2"
。
发布于 2018-02-08 07:30:38
我发现你的类和在OGM中的使用有两个问题:
Entity
这个标签。如果OGM试图从Neo4j加载数据,这将产生问题。它找不到正确的类型来赋值。可能的解决方法:- Explicitly set the label to another one like `StubEntity` for this class.
- If this is not possible because the uniqueness of the uuid over both classes, you may not even need the `StubEntity` but use the `FullEntity` class also for the relationship target. There will be no difference in Neo4j after saving the data.
- If both classes have more differences than the sample code above shows, you may create an abstract class with the `Entity` label and also provide special type labels (implicit by just annotating with `@NodeEntity` or explicit with the label attribute you are already using) on the implementing classes. Than you could use the uuid constraint in the abstract class.
两次使用@Id
注释的
@Id
。https://stackoverflow.com/questions/48641888
复制