温馨提示:本文翻译自stackoverflow.com,查看原文请点击:spring - Repository save() is not working
neo4j neo4j-ogm spring spring-data-neo4j spring-data-neo4j-4

spring - 储存库save()无法运作

发布于 2020-06-16 13:00:53

我目前正在使用spring-data-neo4j并在持久化数据方面有一个很奇怪的行为。

我阅读了入门指南,并仔细阅读了《良好关系:Spring Data Neo4j指南》在消除较小的问题和缺陷(例如使用spring-ogm 1.1.4摆脱了neo4j-server依赖性)之后,加载现有节点即可工作。

让我们看一下我的代码...

这是实体:

package sdn.test.model;

import org.neo4j.ogm.annotation.GraphId;
import org.neo4j.ogm.annotation.NodeEntity;

@NodeEntity
public class TestUser {

    @GraphId
    private Long id;

    private String username;
    private String password;

    public TestUser() {
    }

    public TestUser(Long id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TestUser testUser = (TestUser) o;

        if (getId() != null ? !getId().equals(testUser.getId()) : testUser.getId() != null) return false;
        if (getUsername() != null ? !getUsername().equals(testUser.getUsername()) : testUser.getUsername() != null)
            return false;
        return getPassword() != null ? getPassword().equals(testUser.getPassword()) : testUser.getPassword() == null;

    }

    @Override
    public int hashCode() {
        int result = getId() != null ? getId().hashCode() : 0;
        result = 31 * result + (getUsername() != null ? getUsername().hashCode() : 0);
        result = 31 * result + (getPassword() != null ? getPassword().hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "TestUser{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                "}";
    }
}

这是我的存储库:

package sdn.test.repository;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import sdn.test.model.TestUser;

@Repository
public interface UserRepository extends GraphRepository<TestUser> {

    @Query("MATCH (user:TestUser{username: {username}, password: {password}}) RETURN user")
    TestUser findByUsernameAndPassword(@Param("username") String username, @Param("password") String password);

}

这是neo4j配置:

package sdn.test.config;

import org.neo4j.ogm.session.SessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.neo4j.config.Neo4jConfiguration;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;
import org.springframework.data.neo4j.server.Neo4jServer;
import org.springframework.data.neo4j.server.RemoteServer;
import org.springframework.transaction.annotation.EnableTransactionManagement;


@Configuration
@EnableNeo4jRepositories("sdn.test.repository")
@EnableTransactionManagement
public class Neo4jConfig extends Neo4jConfiguration {

    @Bean
    @Override
    public Neo4jServer neo4jServer() {
        return new RemoteServer("http://localhost:7474", "neo4j", "test");
    }

    @Bean
    @Override
    public SessionFactory getSessionFactory() {
        return new SessionFactory("sdn.test.model");
    }

}

一切都放在一个简单的Spring Boot应用程序中,我尝试在此测试类中创建实体:

package sdn.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import sdn.test.config.Neo4jConfig;
import sdn.test.model.TestUser;
import sdn.test.repository.UserRepository;

import java.util.Date;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {
        Neo4jConfig.class})
public class SimpleNeo4jTests {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void createNewUser() {
        long timeOffset = (new Date()).getTime();
        String username = "test" + timeOffset;
        String password = "password@" + timeOffset;

        TestUser newUser = new TestUser(timeOffset, username, password);
        userRepository.save(newUser);

        // Try to load the user
        TestUser actualUser = userRepository.findByUsernameAndPassword(username, password);

        assertThat(actualUser, equalTo(newUser));
    }
}

最后但并非最不重要的,这是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>net.h0lg.test</groupId>
    <artifactId>simple-sdn4-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.2.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm</artifactId>
            <version>1.1.4</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
            <version>4.0.0.RELEASE</version>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

当我致电userRepository.save()并检查“远程”服务器确认红色测试结果时,没有引发任何错误

明确给标签名称提供@GraphEntity(label = "TestUser")帮助没有帮助。显式使用事务也无济于事。

任何想法和提示都将受到高度赞赏。

查看更多

提问者
incredibleholg
被浏览
9
Luanne 2016-02-02 21:22

看起来您正在TestUser通过测试设置节点实体的@GraphId

TestUser newUser = new TestUser(timeOffset, username, password);

public TestUser(Long id, String username, String password) {
    this.id = id;
    this.username = username;
    this.password = password;
}

应用程序代码永远不要为@GraphId分配值。您可以删除它,看看是否有帮助?