gjrwebber / spring-data-gremlin

Spring data gremlin makes it easier to implement Graph based repositories. This module extends Spring Data to allow support for potentially any Graph database that implements the Tinkerpop Blueprints 2.x API.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bug with list Enum

buiminhhuy opened this issue · comments

Enum:
public enum Role {
USER,
ADMINISTRATOR
}

Class:
UserAccount{
@Enumerated(STRING)
private Set< Role > roles;
public Set< Role > getRoles() {
return roles;
}
....
//other properties
}

When save by Respository, then I use as below:

Set< Role > roles = userAccount.getRoles();
for (Role role : roles) {
//todo
}

I get Error at statement: "for (Role role : roles) {"
java.lang.ClassCastException: java.lang.String cannot be cast to <package_name>.Role

This has been fixed by saving the Collection of Enums as a comma separated String in the Graph. So in the example above a String "USER,ADMINISTRATOR" will be saved to the graph if the Set contains both enums.

Also, the Collection declaration must be a concrete type, unless you use the new @EnumeratedCollection annotation where you can specify the concrete type of the Collection. So in the example above, you can change the declaration to:

private HashSet< Role > roles;

// OR

@EnumeratedCollection(HashSet.class)
private Set< Role > roles;