Search code examples
javaneo4jneo4j-ogm

searching for object with custom id


I have a class that looks like this

@Data
@NodeEntity
public class StoryCharacter {
    @Index(unique = true)
    private String agnosticId;
    private String name;

    @Relationship(type = "FAMILIAR_WITH")
    private Set<StoryCharacter> acquaintances;
}

I needed a custom ID that is not related to the default long id. So I introduced a field and set it as index.

But how to find the object by that id?

I wanted to do it like this

session.openSession().load(StoryCharacter.class, "custom_id")

but it fails with error that it must be Long. I assume that maybe I need to use Filter object for search by that id. Or is there another way?


Solution

  • If you want to use a custom id the field has to be annotated with @Id instead of @Index(unique=true). In cases you do not want to set the id manually, there is an option to provide a id generation strategy (more details in the documentation.

    You are seeing this error because Neo4j-OGM cannot determine what type your id field has and falls back to the standard Long. If you define your id as mentioned above, the load will work.