1

Neo4j needs an id field to work which is of type Long. This works well with Spring data neo4j. I would like to have another field of type UUID and have T findOne(T id) to work with my UUID not neo generated Ids.

As I am using Spring Data Rest, I don't want to expose neo's id in the URL.

http://localhost:8080/resource/{neoId}

to

http://localhost:8080/resource/{uuid}

Any ideas if this is possible?

UPDATED

{
    name: "Root",
    resourceId: "00671e1a-4053-4a68-9c59-f870915e3257",
    _links: {
    self: {
        href: "http://localhost:8080/resource/9750"
    },
    parents: {
         href: "http://localhost:8080/resource/9750/parents"
    },
    children: {
        href: "http://localhost:8080/resource/9750/children"
              }
     }
 }
Chirdeep Tomar
  • 4,281
  • 8
  • 37
  • 66

2 Answers2

2

When it comes to the finder method in your repository you are at liberty to either override the one provided in the CrudRepository in your own interface, or provide an alternative which may be T findByUuid(Long uuid).

Depending on how you are modelling your classes you can rely on the derived query from your method name, or you can annotate with a query, something like:

@Query(value = "MATCH (n:YourNodeType{uuid:{0}) RETURN n")

If you are going to be using a specific UUID class then you will need to tell Neo how to persist the UUID value. If you are will store it as a String (as seems sensible) then I believe there is no need to annotate the field, anything else then you will need a GraphProperty annotation:

@GraphProperty(propertyType = Long.class)

Again depending on the class of UUID you may need to register a conversion class with Spring which implements the org.springframework.core.convert.converter.Converter interface and converts between your domain class type (UUID) and the stored type (String).

Or, just convert the UUID to a string and store it yourself and don't worry about all the conversion.

Whatever you do, make sure that your new uuid is indexed and presumably unique.

JohnMark13
  • 3,709
  • 1
  • 15
  • 26
  • Thanks John, I got the finder method to work http://localhost:8080/resource/00671e1a-4053-4a68-9c59-f870915e3257 which is what I wanted. However, the link to self, parent and children still have node id see the updated question. This is obvious but is there a way to change this to UUID as well – Chirdeep Tomar Jan 27 '15 at 12:18
0

You can add a String attribute to your entities, calling it uuid, and simply declare a E findByUuid(String uuid) in your Repository for E, Spring Data will automatically generate code for it. For example:

@NodeEntity
public class Entity {
    ...
    @Indexed
    private String uuid;
    ...
    public String getUuid() {
        return uuid;
    }
    void setUuid(String uuid) {
        this.uuid = uuid;
    }
    ...
}

public interface EntityRepository extends GraphRepository<Entity> {
    ...
    Entity findByUuid(String uuid);
    ...
}
remigio
  • 4,101
  • 1
  • 26
  • 28