【问题标题】:Spring Data Neo4j: save new nodes and new relationship and Transitive PersistenceSpring Data Neo4j:保存新节点和新关系以及传递持久性
【发布时间】: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


    【解决方案1】:

    我解决了 Failed 2 的问题 将 Bs 保存到 DB 应该在保存包含 B 作为关系端节点的 A 之前完成。然后将创建从 A 到 B 的 Annotation RelatedTo。尝试保存A时如果B不存在,会报null异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多