Search code examples
neo4jspring-data-neo4jspring-data-neo4j-4

Cannot use enum in repository query (neo4j/Spring Data)


I'm having a problem querying based on an Enum property of my NodeEntity.

The NodeEntity in question is defined:

@NodeEntity(label = "Entity")
public class MyEntity {

    @GraphId
    private Long internalId;

    ....

    private State state;

    @Transient
    public enum State {
        STATEONE, STATETWO, STATETHREE
    }
    ....

It saves without a problem, the state Enum represented perfectly, and I can query using other properties (Strings) with no problem at all. However the problem is the following query in a repository:

@Query("MATCH (entity:Entity {state:{0}})" +
       "RETURN entity")
List<MyEntity> findByState(MyEntity.State state)

i.e. find all entities with the given state.

There's no exception, however using this simply returns a List of 0 Entities.

I've tried all kinds of variations on this, using a WHERE clause for example, with no luck.

The Entities are persisted properly, using findAll() in the same test returns the expected List of Entities with their states exactly as I would expect.

Any thoughts?


Solution

  • Not quite sure what the value @Transient adds to the enum. It is anyway not persistable as a node or relationship in Neo4j. It is sufficient to define the field as one that should persist with

    private State state;

    and leave off the @Transient annotation from the enum. With it, SDN ignores the field sent to the derived query.

    However, if you have a good reason to mark the enum @Transient, please do share it and we'll re-visit this case.