如何在spring数据neo4j 4.1.2中添加新的关系

时间:2016-09-20 12:12:01

标签: spring neo4j spring-data-neo4j spring-data-neo4j-4 neo4j-ogm

我已经关注了此Tutorial

我想在上面的教程已经创建的Meat类别中添加名为Beef的新成分。这是我添加新成分的代码:

    Category categoryMeat = categoryRepository.findByName("Meat");
    Ingredient ingredientBeef = ingredientRepository.findByName("Beef");
    ingredientBeef.setCategory(categoryMeat);
    ingredientRepository.save(ingredientBeef);

然后我把牛肉和胡萝卜配对:

    Pairing pairingNew = new Pairing();
    pairingNew.setFirst(ingredientBeef);
    pairingNew.setSecond(carrot);
    pairingNew.setAffinity("Delicious");
    pairingRepository.save(pairingNew);

然后我使用以下代码删除牛肉:

    Ingredient deletedBeef = ingredientRepository.findByName("Beef");
    ingredientRepository.delete(deletedBeef);

然后我尝试使用此代码添加新成分并将羊肉与胡萝卜配对:

    Category newCategoryMeat = categoryRepository.findByName("Meat");
    Ingredient ingredientLamb = new Ingredient("Lamb");
    ingredientLamb.setCategory(newCategoryMeat);
    ingredientRepository.save(ingredientLamb);

    Pairing pairingNewLamb = new Pairing();
    pairingNewLamb.setFirst(ingredientLamb);
    pairingNewLamb.setSecond(carrot);
    pairingNewLamb.setAffinity("Delicious");
    pairingRepository.save(pairingNewLamb);

当我尝试保存新成分羊肉时,我收到此错误:

Error executing Cypher "Neo.ClientError.Statement.EntityNotFound"; Code: Neo.ClientError.Statement.EntityNotFound; Description: Relationship with id 6

如何解决?

供您参考:我正在使用spring data neo4j 4.1.2和neo4j community edition 3.0.6

这是我的pom依赖:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>   

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-neo4j</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
    </dependency>
</dependencies>

0 个答案:

没有答案