[英]Spring Data Neo4j: save new nodes and new relationship and Transitive Persistence
I try to save a new entity who contains another new entity of different type and also a new relationship between them and I failed. 我尝试保存一个新实体,其中包含另一个不同类型的新实体,并且它们之间也存在新的关系,但我失败了。 Basically I hope to understand Transitive Persistence . 基本上我希望了解传递持久性 。
Spring Data Neo4j version : 3.3.2.RELEASE Spring Data Neo4j版本 :3.3.2.RELEASE
Neo4j Server : neo4j-community-2.2.3 Neo4j服务器 :neo4j-community-2.2.3
Here is what I have tested: 这是我测试过的:
SUCCEED : save new entities seperately and then create/save a relationship Entity A, Entity B, Relationship C between A & B 成功 :分别保存新实体,然后创建/保存关系A和B之间的实体A,实体B,关系C
FAILED1 : save new entities seperately, then create several relationship of same type and save Entity A1, A2, Entity B1, B2, Relationship C1, C2. FAILED1 :分别保存新实体,然后创建几个相同类型的关系,并保存实体A1,A2,实体B1,B2,关系C1,C2。 Then A1-C1-B1, A1-C2-B2, A2-C1-B2 然后是A1-C1-B1,A1-C2-B2,A2-C1-B2
Result: I got off course entitie As and Bs but no relationship Cs. 结果:我偏离了As和B的航向,但没有C的关系。
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
Exception Log: 异常日志:
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 : save a new entity A, creat two entities B and set to A, save A again, I got the first entity B saved with the right relationship but then an exception occurs and the second entity B isn't saved. FAILED2 :保存一个新实体A,创建两个实体B并设置为A,再次保存A,我得到了第一个具有正确关系的实体B,但是发生了异常,并且没有保存第二个实体B。
Entity A: 实体A:
@NodeEntity
public class Medicine {
@GraphId Long id;
@RelatedTo(type = "HEAL")
private Set<Symptom> symptom;
...
}
Entity B: 实体B:
@NodeEntity
public class Symptom {
@GraphId Long nodeId;
...
}
Controller: 控制器:
@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);
...
}
Exception Log: 异常日志:
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)
I solve to problem for Failed 2 Saving Bs to DB should be done before saving A who contains B as a relationship-end node. 我解决了“ 失败2”的问题,应先将B保存到DB,然后再将包含B的A保存为关系端节点。 Then the Annotation RelatedTo from A to B will be created. 然后将创建从A到B的Annotation RelatedTo 。 If B doesnot exist when trying to save A, null exception will be reported. 如果尝试保存A时B不存在,则将报告null异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.