我尝试保存一个新实体,该实体包含另一个不同类型的新实体,以及它们之间的新关系,但我失败了。基本上我希望理解传递持久性。
Spring Data Neo4j版本:3.3.2.RELEASE
Neo4j服务器:neo4j-community-2.2.3
以下是我测试的内容:
成功:单独保存新实体,然后创建/保存关系 实体A,实体B,A& A之间的关系C. B
FAILED1 :单独保存新实体,然后创建多个相同类型的关系并保存 实体A1,A2,实体B1,B2,关系C1,C2。然后是A1-C1-B1,A1-C2-B2,A2-C1-B2
结果:我离开了As和Bs,但没有关系Cs。
FuncModerate fm1 = new FuncModerate(m, s, "HEAL");
FuncModerate fm2 = new FuncModerate(m2, s1, "HEAL");
FuncModerate fm3 = new FuncModerate(m3, s1, "HEAL");
Set<FuncModerate> fms = new HashSet<FuncModerate>();
fms.add(fm1);
fms.add(fm2);
fms.add(fm3);
neo4jOps.save(fms); //Exception occurs
异常日志:
java.lang.NullPointerException
at org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:549)
at org.springframework.data.neo4j.support.Neo4jTemplate.getMappingPolicy(Neo4jTemplate.java:726)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:354)
at org.ming.controller.Neo4JController.newF(Neo4JController.java:72)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
FAILED2 :保存一个新实体A,创建两个实体B并设置为A,再次保存A,我得到了第一个实体B保存了正确的关系,但后来发生了异常而第二个实体B未保存。
实体A:
@NodeEntity
public class Medicine {
@GraphId Long id;
@RelatedTo(type = "HEAL")
private Set<Symptom> symptom;
...
}
实体B:
@NodeEntity
public class Symptom {
@GraphId Long nodeId;
...
}
控制器:
@RequestMapping(value = "/new", method = RequestMethod.POST)
public void newF() {
Medicine m = new Medicine("moderate hungry");
neo4jOps.save(m);
Symptom s = new Symptom("much confidence");
Symptom s2 = new Symptom("less angry");
Set<Symptom> ss = new HashSet<Symptom>();
ss.add(s); // got saved
ss.add(s2); // not got saved
m.setSymptom(ss);
neo4jOps.save(m);
...
}
异常日志:
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/springlearn] threw exception [Request processing failed; nested exception is org.neo4j.graphdb.NotFoundException: '__type__' on http://localhost:7474/db/data/relationship/14] with root cause
org.neo4j.graphdb.NotFoundException: '__type__' on http://localhost:7474/db/data/relationship/14
at org.neo4j.rest.graphdb.entity.RestEntity.getProperty(RestEntity.java:125)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexBasedTypeRepresentationStrategy.readAliasFrom(AbstractIndexBasedTypeRepresentationStrategy.java:126)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:36)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.readAliasFrom(TRSTypeAliasAccessor.java:26)
at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:102)
at org.springframework.data.convert.DefaultTypeMapper.getDefaultedTypeToBeUsed(DefaultTypeMapper.java:165)
at org.springframework.data.convert.DefaultTypeMapper.readType(DefaultTypeMapper.java:142)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.read(Neo4jEntityConverterImpl.java:77)
答案 0 :(得分:0)
我解决了失败2 的问题 在将包含B作为关系结束节点的A保存之前,应该将Bs保存到DB。然后将创建从A到B的注释 RelatedTo 。如果在尝试保存A时不存在B,则将报告空异常。