代码之家  ›  专栏  ›  技术社区  ›  incredibleholg

存储库save()不工作

  •  0
  • incredibleholg  · 技术社区  · 10 年前

    我目前正在玩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 回复  |  直到 10 年前
        1
  •  3
  •   Luanne    10 年前

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

    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赋值。你能把它去掉,看看是否有用吗?

    推荐文章