将 UUID 添加到 neo4j spring 数据框架

adding a UUID to a neo4j spring data framework

背景 如果我使用 Spring 数据,我想找出将 UUID 添加到 neo4j 的正确方法。

我看过: https://dzone.com/articles/assigning-uuids-neo4j-nodes 这里 TransactionEventHandler 用于在必要时插入 UUID。但是制作这个教程的人并没有使用 spring 数据。

我也看到了这个人的代码:https://github.com/spring-projects/spring-data-neo4j/blob/master/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/web/domain/User.java他们好像是在用java的java.util.UUID,然后只是把它转换成一个字符串,作为一个字符串实体来使用并对其进行索引并从那里开始。这似乎是最简单的方法。

但是,在文档中:https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/他们似乎使用 UUID 作为 AddUuidPreSaveEventListener

用例的示例

问题

我应该使用哪种方法来添加 UUID?

我可以添加

...
import java.util.UUID;

import org.neo4j.ogm.annotation.Index;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import org.neo4j.ogm.typeconversion.UuidStringConverter;

...

@Convert(UuidStringConverter.class)
@Index(unique = true, primary = true)
private UUID uuid = UUID.randomUUID();

...

添加到我的 GraphType.java 文件并称之为好?

注意:我对所有这些技术都很陌生,可能还没有经验,甚至无法就这些问题提出正确的问题。

注2: 我以前看过 graphaware UUID 库,它看起来确实是最新的,但我认为如果我正在处理 spring 数据,可能会有一种制作 UUID 的首选方法。

您可以使用 GraphAware Neo4j UUID 库。

文档说:

GraphAware UUID is a simple library that transparently assigns a UUID to newly created nodes and relationships in the graph and makes sure nobody can (accidentally or intentionally) change or delete them.

只需下载 neo4j.conf 文件中的 GraphAware Neo4j Framework and GraphAware Neo4j UUID .jar files to /plugins directory, modify few lines 并重新启动 Neo4j。之后,UUIDs 将分配给在 Neo4j 图中创建的每个节点/关系。

此方法不依赖于 Spring 或 Spring 数据。

现在有一种在 spring 数据 neo4j 中生成 UUID 的官方方法是使用 UUIDStringGenerator。你可以这样使用它

    @Id
    @GeneratedValue(generatorClass = UUIDStringGenerator.class)
    private String id;

然后在您的 neo4j 存储库中将其作为字符串使用。