Spring - Neo4j Database : Save Enum String to Database and Return as Enum

983 views Asked by At

I want save that Enum in my Database, but only the value, not the name. For example I save object with CODE_3, so in my Database, Should be ActCode : "3", not ActCode : "CODE_3". But when I find the object, it should be return as ActCode.CODE_3, not as String "3".

I have an Enum like this :

public enum ActCode {

        CODE_3("3"),
        CODE_4("4"),
        CODE_6("6"),
        CODE_7("7"),
        CODE_12("12"),
        CODE_13("13"),
        CODE_14("14"),
        CODE_15("15"),
        CODE_16("16"),
        CODE_17("17"),
        CODE_18("18"),
        CODE_19("19"),
        CODE_20("20"),
        CODE_23("23")

        private String value;

        private ActCode(String value) {
            this.value = value;
        }

        @Override
        public String toString() {
            return this.value;
        }
}

This is my Class :

@NodeEntity
public class TestEnum {

    @GraphId
    Long graphId;

    String id;

    String name;

    @Enumerated(EnumType.STRING) 
    ActCode actCode;

}

This is how I save my Class in my Service :

TestEnum test = new TestEnum("Enum 1",ActCode.CODE_3);
testEnumRepository.save(test);

When I look at my Database, this is the result : enter image description here

I also tried to use converter, this is my reference

I'm using Spring 4.3.3.RELEASE, Hibernate 5.0.11.Final, SDN4.1.3-RELEASEand neo4j-ogm-2.0.5.

1

There are 1 answers

0
digx1 On

At the moment using @Convert will not apply the converter on java.util.Enum as the OGM has a default converter which at the moment, cannot be overridden.

If you need to get something working then you can make ActCode a @NodeEntity and build the same values for the enum in the database itself.

It's not perfect but it will at least solve the issue till this functionality is added. In the mean time you could raise an enhancement request with the Neo4j OGM Github to allow custom converters to work with enums.

Also FYI: Neo4j-OGM and Spring Data Neo4J do not support JPA annotations like @Enumerated.