首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Neo4j OGM更新节点而不先检索它

使用Neo4j OGM更新节点而不先检索它
EN

Stack Overflow用户
提问于 2018-02-06 19:23:57
回答 1查看 188关注 0票数 1

我有一个结构,它(简化的)看起来像这样:

代码语言:javascript
运行
复制
@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对象时,如果我以一种方式保存,那么它都可以工作:

代码语言:javascript
运行
复制
entityRepository.save(FullEntity().apply {
    uuid = "uuid1"
    someMoreData = "SomeMoreData1"
    target = StubEntity().apply {
        uuid = "uuid2"
    }
})
// some time later ...
entityRepository.save(FullEntity().apply {
    uuid = "uuid2"
    someMoreData = "SomeMoreData2"
})

但是如果我像这样颠倒顺序:

代码语言:javascript
运行
复制
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"

EN

回答 1

Stack Overflow用户

发布于 2018-02-08 15:30:38

我发现你的类和在OGM中的使用有两个问题:

  1. 你两次使用Entity这个标签。如果OGM试图从Neo4j加载数据,这将产生问题。它找不到正确的类型来赋值。可能的解决方法:

代码语言:javascript
运行
复制
- 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注释的

  1. 。如果只需要索引中的字段和唯一约束(如果我理解正确的话),则不需要在uuid字段上声明额外的@Id
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48641888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档