【发布时间】:2015-07-31 10:44:20
【问题描述】:
我尝试保存一个新实体,该实体包含另一个不同类型的新实体以及它们之间的新关系,但我失败了。基本上我希望了解Transitive Persistence。
Spring Data Neo4j 版本:3.3.2.RELEASE
Neo4j 服务器:neo4j-community-2.2.3
这是我测试过的:
成功:单独保存新实体,然后创建/保存关系 实体A,实体B,A和B之间的关系C
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)
【问题讨论】:
标签: java neo4j spring-data-neo4j