javaspringneo4jenums

Spring Data Neo4j: findAll fails with "Required property $enum$name not found" when using enum as a @Node


I am using Spring Data Neo4j to work with my Neo4j database. My application includes a Variation node entity that has a relationship with an enum called Type. Here is a simplified version of my code:

@Node
public class Variation extends BaseEntity {
    private String title;

    @Relationship(type = "HAS_ATTRIBUTE", direction = Relationship.Direction.OUTGOING)
    private Type type;

    // Other fields and relationships...
}
@Node
public enum Type {
    CUSTOM(0),
    FIX(1),
    // Other constants...

    @Id
    private final int id;

    Type(int id) {
        this.id = id;
    }
}

Here is my build.gradle dependencies:

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-data-neo4j'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'

    implementation 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
}

I am also using a VariationService to create nodes and relationships. The findAll method on the VariationRepository fails with the following error:

java.lang.IllegalStateException: Required property $enum$name not found for class org.com.example.node.Type
    at org.springframework.data.mapping.PersistentEntity.getRequiredPersistentProperty
    ...

The error seems to indicate an issue with the Type enum mapping.

My question is:


Solution

  • @Node is supposed to be used to annotate a class that represents nodes in the database, with each node representable as a separate instance of that class. Since an enum just contains a fixed set of constant values (and you can't create different instances on an enum class), it does not make sense to treat an enum as a node.

    To fix your code, you can just remove the @Node annotation from the enum, and remove the @Relationship annotation as well. That would directly store the type as a property of each Variation node. And, if necessary, you can optimize finding all the Variation nodes of any given type by creating an index on the Variation and type combination.