Romulus
Romulus

Reputation: 1694

adding a UUID to a neo4j spring data framework

Background I am trying to figure out the correct way to add a UUID to neo4j if I am using Spring Data.

I have seen: https://dzone.com/articles/assigning-uuids-neo4j-nodes and here a TransactionEventHandler is used to insert a UUID when necessary. But the person who made this tutorial was not using spring data.

I have also seen this person's code: https://github.com/spring-projects/spring-data-neo4j/blob/master/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/web/domain/User.java They seem to be using java's java.util.UUID and then just converting it to a string and using it as a string entity and indexing it and going from there. This seems to be the simplest way.

But, in the docs: https://docs.spring.io/spring-data/neo4j/docs/current/reference/html/ They seem to using UUID's as their example for a usecase of an AddUuidPreSaveEventListener

Questions

Which method should I use for adding UUID's?

Could I just add

...
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();

...

to my GraphType.java file and call it good?

Note: I am very new to all these technologies and could be still too inexperienced to even properly ask a question on this stuff.

Note 2: I have seen the graphaware UUID library before, it does seem to be fairly up to date, but I assumed that there may be a preferred way to make UUIDs if I was working with spring data.

Upvotes: 4

Views: 1943

Answers (2)

Abdullah Khaled
Abdullah Khaled

Reputation: 338

There is now an official way to generate UUID in spring data neo4j is by using UUIDStringGenerator. you can use it like this

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

Then use it in your neo4j repository as a string.

Upvotes: 2

Bruno Peres
Bruno Peres

Reputation: 16375

You can use the GraphAware Neo4j UUID library.

The docs says:

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.

Simply download GraphAware Neo4j Framework and GraphAware Neo4j UUID .jar files to /plugins directory, modify few lines in neo4j.conf file and restart Neo4j. After it, UUIDs will be assigned to each node / relationships created in the Neo4j graph.

This approach does not depends on Spring or Spring Data.

Upvotes: 6

Related Questions