在我的@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]