在下面的@RelationshipEntity中,您可以看到我有一个Set<Right> rights (一组权限)。Right是一个Emum。Neo4J只允许我保存一组String,所以我创建了一个自定义的转换器。
@RelationshipEntity (type="LOGIN")
public class Login {
@GraphId
Long id;
@StartNode
Person person;
@EndNode
Organisation organisation;
@Property
String role;
@Property
@Convert(RightConverter.class)
Set<Right> rights = new HashSet<Right>();
public Login() {
// Empty Constructor
}
/* Getters and Setters */
}这一切对我来说都是有意义的,但是当我运行应用程序时,我的RightConverter类出现错误。
public class RightConverter implements AttributeConverter<Set<Right>, Set<String>> {
public Set<String> toGraphProperty(Set<Right> rights) {
Set<String> result = new HashSet<>();
for (Right right : rights) {
result.add(right.name());
}
return result;
}
public Set<Right> toEntityAttribute(Set<String> rights) {
Set<Right> result = new HashSet<>();
for (String right : rights) {
result.add(Right.valueOf(right));
}
return result;
}
}它适用于保存,但不适用于加载:
nested exception is org.neo4j.ogm.metadata.MappingException: Error mapping GraphModel to instance of com.noxgroup.nitro.domain.Person
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to java.util.Set
at com.noxgroup.nitro.domain.converters.RightConverter.toEntityAttribute(RightConverter.java:9) ~[main/:na]发布于 2015-05-31 00:29:22
如果您使用的是SDN4的最新快照(即在M1发布之后),则不需要为枚举的集合或数组编写转换器。默认的枚举转换器将为您将一组枚举转换为字符串数组。
但是,如果您使用的是早期版本(M1),则此支持不存在,因此您确实需要编写一个转换器。在这种情况下,RightConverter只需更改为转换为字符串数组,这是Neo4j最终将使用的,而不是集合。这是可行的:
public class RightConverter implements AttributeConverter<Set<Right>, String[]> {
public String[] toGraphProperty(Set<Right> rights) {
String[] result = new String[rights.size()];
int i = 0;
for (Right right : rights) {
result[i++] = right.name();
}
return result;
}
public Set<Right> toEntityAttribute(String[] rights) {
Set<Right> result = new HashSet<>();
for (String right : rights) {
result.add(Right.valueOf(right));
}
return result;
}
}https://stackoverflow.com/questions/30512208
复制相似问题