我正在研究Neo4j Spring Data项目,并开始使用“RelationshipEntity”和“@RelatedToVia”关系来更好地建模一些关系。
我遇到的问题是如果我运行嵌入式数据库一切都很好但是当我使用SpringCypherRestGraphDatabase运行时,我不断收到错误“错误添加元素...索引__rel_types __”:
任何指针都会很棒!!
堆栈跟踪:
Caused by: java.lang.RuntimeException: Error adding element 52 className AlsoKnownAs to index __rel_types__
at org.neo4j.rest.graphdb.RestAPIImpl.addToIndex(RestAPIImpl.java:703)
at org.neo4j.rest.graphdb.RestAPICypherImpl.addToIndex(RestAPICypherImpl.java:683)
at org.neo4j.rest.graphdb.index.RestIndex.add(RestIndex.java:58)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexBasedTypeRepresentationStrategy.add(AbstractIndexBasedTypeRepresentationStrategy.java:139)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexBasedTypeRepresentationStrategy.addToTypesIndex(AbstractIndexBasedTypeRepresentationStrategy.java:131)
at org.springframework.data.neo4j.support.typerepresentation.AbstractIndexBasedTypeRepresentationStrategy.writeTypeTo(AbstractIndexBasedTypeRepresentationStrategy.java:74)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.writeTypeTo(TRSTypeAliasAccessor.java:46)
at org.springframework.data.neo4j.support.mapping.TRSTypeAliasAccessor.writeTypeTo(TRSTypeAliasAccessor.java:26)
at org.springframework.data.convert.DefaultTypeMapper.writeType(DefaultTypeMapper.java:199)
at org.springframework.data.convert.DefaultTypeMapper.writeType(DefaultTypeMapper.java:186)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:168)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:179)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:254)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:235)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:365)
at org.springframework.data.neo4j.fieldaccess.RelatedToViaCollectionFieldAccessorFactory$RelatedToViaCollectionFieldAccessor.persistEntities(RelatedToViaCollectionFieldAccessorFactory.java:99)
at org.springframework.data.neo4j.fieldaccess.RelatedToViaCollectionFieldAccessorFactory$RelatedToViaCollectionFieldAccessor.setValue(RelatedToViaCollectionFieldAccessorFactory.java:93)
at org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet.updateValue(ManagedFieldAccessorSet.java:112)
at org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet.update(ManagedFieldAccessorSet.java:100)
at org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet.add(ManagedFieldAccessorSet.java:126)
RelationshipEntity
import org.springframework.data.neo4j.annotation.EndNode;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.RelationshipEntity;
import org.springframework.data.neo4j.annotation.StartNode;
@RelationshipEntity(type = "AKA")
public class AlsoKnownAs {
@GraphId Long id;
String name;
@Fetch @StartNode private Person identity;
@Fetch @EndNode private Person pseudenom;
public AlsoKnownAs setNickname(String nickName){
this.name = nickName;
return this;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Person getIdentity() {
return identity;
}
public void setIdentity(Person identity) {
this.identity = identity;
}
public Person getPseudenom() {
return pseudenom;
}
public void setPseudenom(Person pseudenom) {
this.pseudenom = pseudenom;
}
}
节点实体
import java.util.HashSet;
import java.util.Set;
import org.neo4j.graphdb.Direction;
import org.springframework.data.neo4j.annotation.Fetch;
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.NodeEntity;
import org.springframework.data.neo4j.annotation.RelatedTo;
import org.springframework.data.neo4j.annotation.RelatedToVia;
@NodeEntity
public class Person {
@GraphId Long id;
public String name;
public Person() {}
public Person(String name) { this.name = name; }
@RelatedTo(type="TEAMMATE", direction=Direction.BOTH)
public @Fetch Set<Person> teammates;
@RelatedToVia(type="AKA")
Set<AlsoKnownAs> identities;
public void worksWith(Person person) {
if (teammates == null) {
teammates = new HashSet<Person>();
}
teammates.add(person);
}
public String toString() {
String results = name + "'s teammates include\n";
if (teammates != null) {
for (Person person : teammates) {
results += "\t- " + person.name + "\n";
}
}
return results;
}
public void identify(Person person, String nickName) {
AlsoKnownAs aka = new AlsoKnownAs();
aka.setIdentity(this);
aka.setPseudenom(person);
aka.setName(nickName);
this.identities.add(aka);
}
}
POM :
<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>com.teralitic</groupId>
<artifactId>geonames</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<properties>
<spring-data-releasetrain.version>Fowler-RELEASE</spring-data-releasetrain.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>3.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j-rest</artifactId>
<version>3.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</repository>
<repository>
<id>neo4j</id>
<name>Neo4j</name>
<url>http://m2.neo4j.org/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
被修改 我可以看到在旧的Spring Data构建中发生了同样的问题: https://build.spring.io/browse/SPRINGDATA-NIGHTLY-JOB1-697/test/case/92176623;jsessionid=5EE45702C67548804FEF985C00F5DAD2
仍在调查可能导致这种情况的原因。
答案 0 :(得分:2)
还是无法想办法彻底解决这个问题。
到目前为止我唯一的解决方法是替换
org.springframework.data.neo4j.rest.SpringCypherRestGraphDatabase
现已弃用
org.springframework.data.neo4j.rest.SpringRestGraphDatabase
我现在能够保存@RelatedToVia关系。
答案 1 :(得分:0)
我在这里遇到了完全相同的问题。
我使用spring-data-neo4j 3.3.0.RELEASE和spring-data-neo4j-rest 3.3.0.RELEASE,我还要手动添加spring-data-commons 1.10(https://jira.spring.io/browse/DATAGRAPH-635)
使用弃用的类解决了这个问题,但我不确定我的配置,因为Michael Hunger说(这里http://java.dzone.com/articles/spring-data-neo4j-330)使用SpringCypherRestGraphDatabase ....