存储库save()无效

时间:2016-02-02 10:43:40

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

我目前正在使用spring-data-neo4j,并且在持久化数据方面有一种非常奇怪的行为。

我阅读了Getting Started guide并查看了Good Relationships: The Spring Data Neo4j Guide Book。在解决了较小的问题和不完善之后加载现有节点(例如使用spring-ogm 1.1.4来摆脱neo4j服务器依赖性)。

让我们来看看我的代码......

这是实体:

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")明确指定标签名称无济于事。明确地使用事务也没有帮助。

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

1 个答案:

答案 0 :(得分:3)

看起来您正在通过测试设置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赋值。你可以删除它,看看它是否有帮助?