Search code examples
neo4jspring-data-neo4jneo4j-ogm

Spring Data Neo4j 4 and primary indexes


I'm trying to understand how to correctly use primary indexes in SDN 4.

From the org.neo4j.ogm.annotation.Index javadoc:

Only one index per class hierarchy may be marked as primary.

Does it mean that if I have a following entity hierarchy:

B extends A
C extends A

I can't add into the both entities B and C the following index definition :

@Index(unique = true, primary = true)
private Long id;

This primary index can be only added to A or to B or to C entity ?

Or if I can add the primary index into the both of them ( B and C), can B.id and C.id hold the same value.. for example 1


Solution

  • Only one index per class hierarchy may be marked as primary.

    This means you can have only 1 primary index in a class, or any of its super classes.

    E.g. if you have following hierarchy:

    class A
    class B extends A
    class C extends B
    

    you can have only 1 index, in either A, B or C.

    In your situation you can have either index in A, or B (or B and C).

    The difference is

    • if the index is in A, you won't be able to create B and C with same id - constraint on A label will be created
    • if the index is in B and C you can create B and C with same id, because it will be 2 separate constraints on 2 separate labels.