简体   繁体   English

如何在Spring Data Graph中持久存储Neo4J NodeEntity之间的关系而不调用两次持久

[英]How to persist relationships between Neo4J NodeEntitys in Spring Data Graph without calling persist twice

The test below fails if I remove the first persist(). 如果删除第一个persist(),下面的测试将失败。 Why do I need to persist the NodeEntity in order for the Set to be instantiated? 为什么我需要保留NodeEntity以便实例化Set? Is there some better way to do this? 有更好的方法吗? I don't want to have to write to the database more often than nessesary. 我不想比必需的更多地写数据库。

 @Test
public void testCompetenceCreation() {
    Competence competence = new Competence();
    competence.setName("Testcompetence");
    competence.persist(); //test fails if this line is removed
    Competence competenceFromDb = competenceRepository.findOne(competence.getId());

    assertEquals(competence.getName(), competenceFromDb.getName());

    Education education = new Education();
    education.setName("Bachelors Degree");
    competence.addEducation(education);
    competence.persist();


    assertEquals(competence.getEducations(), competenceFromDb.getEducations());
}

If i remove the mentioned line, the exception bellow occurs: 如果我删除提到的行,则会发生以下异常:

Throws 投掷

java.lang.NullPointerException
at com.x.entity.Competence.addEducation(Competence.java:54)

Competence.class: 能力等级

@JsonIgnoreProperties({"nodeId", "persistentState", "entityState"})
@NodeEntity
public class Competence {

    @RelatedTo(type = "EDUCATION", elementClass = Education.class)
    private Set<Education> educations;

    public Set<Education> getEducations() {
        return educations;
    }

    public void addEducation(Education education) {
        this.educations.add(education);
    }
}

Education.class 教育课

@JsonIgnoreProperties({"nodeId", "persistentState", "entityState"})
@NodeEntity
public class Education {

    @GraphId
    private Long id;

    @JsonBackReference
    @RelatedTo(type = "COMPETENCE", elementClass = Competence.class, direction = Direction.INCOMING)
    private Competence competence;

    @Indexed
    private String name;

    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

What version of SDN are you running? 您正在运行什么版本的SDN?

Because up until the first persist the entity is detached and AJ doesn't take care of the fields (like creating the managed set). 因为直到第一个持久化实体都分离,并且AJ不会处理字段(例如创建托管集)。 Persist creates the node at connects it to the entity, from then on until the transaction commits your entity is attached and all the changes will be written through. 持久创建节点时将其连接到实体,直到此事务提交您的实体为止,所有更改都将被写入。

It only writes to the db at commit, so no worries about too many writes. 它仅在提交时写入数据库,因此不必担心写入过多。 All the other changes will just be held in memory for your transaction. 所有其他更改将仅保存在您的事务的内存中。 Probably you should also annotate the test method with @Transactional . 也许您还应该使用@Transactional注释测试方法。

Can you create a JIRA issue for this? 您可以为此创建JIRA问题吗? So that a consistent handling is provided. 以便提供一致的处理。 (Problem being that it probably also complains when you initialize the set yourself.) (问题是您自己初始化集合时可能还会抱怨。)

Two other things: 另外两件事:

  • as your relationship between Education<--Competence is probably the same and should just be navigated in the other direction you must provide the same type name in the annotation. 因为您在Education <-能力之间的关系可能是相同的,并且应该以另一个方向浏览,所以您必须在注释中提供相同的 type名称。
  • eg Education<-[:PROVIDES]-Competence 例如教育<-[:PROVIDES]-能力

  • also if you don't call persist your entity will not be created and then the findOne by returning null 同样,如果您不调用持久化,则不会创建您的实体,然后通过返回null来创建findOne

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM