【问题标题】:Springdata Neo4j primary keySpring Data Neo4j 主键
【发布时间】:2014-08-16 16:18:08
【问题描述】:

我是 Neo4j 和 SDN 的新手。我试图使用@Indexed(unique=true) 为我的实体创建主键。它创建唯一节点,但如果我使用索引的相同属性值,它将替换其他属性。 例如:

Class abc{
@Indexed(unique=true)
Long abc_id;
String abc_name;
}

new abc(1,"asd")// creates node with id: 1 name : asd
new abc(1,"xyz")// replaces asd with xyz . 

但是我想抛出一个关于主键违规/重复节点的异常。

有没有办法达到同样的效果?

【问题讨论】:

    标签: neo4j spring-data-neo4j


    【解决方案1】:

    目前,@Indexed 实现不会对 spring data neo4j 论坛上此 JIRA 票证中引用的任何重复项抛出任何异常:here 和此thread

    您可以创建运行密码查询的唯一节点(这会更快)。您可以使用MERGE 创建或更新节点。

    来自文档

    MERGE either matches existing nodes and binds them, or it creates new data and binds that. It’s like a combination of MATCH and CREATE that additionally allows you to specify what happens if the data was matched or created.
    

    因此,如果您想更新任何节点或创建一个节点(如果该模式不存在),您可以执行以下操作。

    MERGE (keanu:Person { name:'Keanu Reeves' })
    ON CREATE SET keanu.created = timestamp()
    ON MATCH SET keanu.lastSeen = timestamp()
    RETURN keanu
    

    【讨论】:

    • 非常感谢。所以使用 MERGE 是实现这一目标的唯一方法,或者对于 Spring-data-neo4j 有一些替代解决方案。
    • 查看答案中附加的线程.. 这家伙尝试了几种方法来实现它,但我更喜欢使用合并创建..
    • 要使用 MERGE 创建或更新节点,您是在 SDN 还是核心 JAVA API 中使用查询注释。我有这个疑问,因为我读到核心 JAVA API 在某些情况下比 SDN 更好。我创建了一个 SDN 项目并使用存储库与数据库进行交互。使用 Neo4jTemplate 而不是存储库会更好吗?提前致谢。
    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-11
    • 1970-01-01
    相关资源
    最近更新 更多