存储库保存()不工作
Repository save() is not working
我目前正在使用 spring-data-neo4j,并且在持久化数据方面有一个非常奇怪的行为。
我读了Getting Started guide and looked through the Good Relationships: The Spring Data Neo4j Guide Book。在摆脱较小的问题和不完美之后(例如使用 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 启动应用程序中,我尝试在这个测试中创建实体 class:
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()
并检查"remote"服务器确认红色测试结果时没有抛出错误。
用 @GraphEntity(label = "TestUser")
明确给出标签名称没有帮助。显式使用事务也无济于事。
非常感谢任何想法和提示。
看起来您正在通过测试设置 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 赋值。您能否将其删除并查看是否有帮助?
我目前正在使用 spring-data-neo4j,并且在持久化数据方面有一个非常奇怪的行为。
我读了Getting Started guide and looked through the Good Relationships: The Spring Data Neo4j Guide Book。在摆脱较小的问题和不完美之后(例如使用 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 启动应用程序中,我尝试在这个测试中创建实体 class:
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()
并检查"remote"服务器确认红色测试结果时没有抛出错误。
用 @GraphEntity(label = "TestUser")
明确给出标签名称没有帮助。显式使用事务也无济于事。
非常感谢任何想法和提示。
看起来您正在通过测试设置 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 赋值。您能否将其删除并查看是否有帮助?