Custom IdStrategy Examples

I'm looking for an example of somebody implementing their own IdStrategy using OGM. I'm trying to write an application that uses entities that are already identifiable by an id, and I'd like to use that id throughout my application.

If I don't have the Neo4j OGM generate it's own Id, my nodes never get saved to the graph.

This is pretty simple to achieve:

You define the id (creation) strategy:

public class MyCustomStrategy implements IdStrategy {
  public Object generateId(Object entity) {
        // your strategy here
        return "myCustomId";

Use this in your domain class:

public class Person {

  @GeneratedValue(strategy = MyCustomStrategy.class)
  private String id;
  /* ... */

and load it with e.g. Neo4j-OGM's Session like this:

session.load(Person.class, "myCustomId")

Ah! It was that last step, loading it in the session that I was missing. Thank you.

I also figured out that if I name the node's @id property something unique (@id myId, instead of @id id) I don't have to use the @GeneratedValue decorator at all and can just pass the external myId to the constructor.

@gerrit.meier, I want to follow up on this now that I've had a chance to revisit it in my project.

It seems that the custom id has to be a String. Is that right?

If I make my id be a String, everything works well. If I make it a Long, the object never gets created in the graph.

The problem is that if I'm using a service interface (following the OGM University tutorial) then I have a conflict when I try to extend the service because the interface is expecting a Long.

Am I missing something?