Neo4J Spring:关系存在疑虑

时间:2013-12-19 12:39:58

标签: neo4j spring-data-neo4j

我正在使用以下环境:

  • neo4j 1.9.5
  • spring 3.2.6
  • spring neo4j 2.3.3

我需要代表路线计划;在我的场景中,我有一些关系,其方向是结果,其他一些方向为INCOMING,另一些方向为方向。为了更清楚,让我假设我有节点A,B,C和D. 让我们假设以下情景

A - &以及c

A< - B

℃下; - > d

我很想在neo4j中代表这种情况;为了做到这一点,我写了这两个类: 我的Node类:

@NodeEntity
public class GraphNode {
//Id del nodo in neo4j
@GraphId
private Long nodeId;
@Indexed(indexName="DB_ID",unique=true,numeric=false)
private String giunzioneDbId;
@Indexed(indexName="COORDINATA_X",unique=false,numeric=true)
private double x;
@Indexed(indexName="COORDINATA_Y",unique=false,numeric=true)
private double y;
@Indexed(indexName="COORDINATA_Z",unique=false,numeric=true)
private double z;
@Indexed(indexName="COORDINATA_M",unique=false,numeric=true)
private double m;
@RelatedToVia(direction=Direction.INCOMING,elementClass=NodesRelationship.class)
private Set<NodesRelationship> versoCareggiataDiscorde = new HashSet<NodesRelationship>();
@RelatedToVia(direction=Direction.OUTGOING,elementClass=NodesRelationship.class)
private Set<NodesRelationship> versoCareggiataConcorde = new HashSet<NodesRelationship>();
@RelatedToVia(direction=Direction.BOTH, elementClass=NodesRelationship.class)
private Set<NodesRelationship> versoCareggiataDoppio = new HashSet<NodesRelationship>();

public Long getNodeId() {
    return nodeId;
}
public void setNodeId(Long nodeId) {
    this.nodeId = nodeId;
}
public String getGiunzioneDbId() {
    return giunzioneDbId;
}
public void setGiunzioneDbId(String giunzioneDbId) {
    this.giunzioneDbId = giunzioneDbId;
}
public double getX() {
    return x;
}
public void setX(double x) {
    this.x = x;
}
public double getY() {
    return y;
}
public void setY(double y) {
    this.y = y;
}
public double getZ() {
    return z;
}
public void setZ(double z) {
    this.z = z;
}
public double getM() {
    return m;
}
public void setM(double m) {
    this.m = m;
}
public Set<NodesRelationship> getVersoCareggiataDiscorde() {
    return versoCareggiataDiscorde;
}
public void setVersoCareggiataDiscorde(Set<NodesRelationship> versoCareggiataDiscorde) {
    this.versoCareggiataDiscorde = versoCareggiataDiscorde;
}
public Set<NodesRelationship> getVersoCareggiataConcorde() {
    return versoCareggiataConcorde;
}
public void setVersoCareggiataConcorde(Set<NodesRelationship> versoCareggiataConcorde) {
    this.versoCareggiataConcorde = versoCareggiataConcorde;
}
public Set<NodesRelationship> getVersoCareggiataDoppio() {
    return versoCareggiataDoppio;
}
public void setVersoCareggiataDoppio(Set<NodesRelationship> versoCareggiataDoppio) {
    this.versoCareggiataDoppio = versoCareggiataDoppio;
}

public void addDiscordantRoadElementGraphRelation(GraphNode endNode, float lunghezzaArco, long elementoStrdaleDbId ){
    NodesRelationship relation = new NodesRelationship(this, endNode, lunghezzaArco, elementoStrdaleDbId);
    if( !versoCareggiataDiscorde.contains(relation) ){
        versoCareggiataDiscorde.add(relation);
    }
}
public void addConcordantRoadElementGraphRelation(GraphNode endNode, float lunghezzaArco, long elementoStrdaleDbId ){
    NodesRelationship relation = new NodesRelationship(this, endNode, lunghezzaArco, elementoStrdaleDbId);
    if( !versoCareggiataConcorde.contains(relation) ){
        versoCareggiataConcorde.add(relation);
    }
}   
public void addTwoWayRoadElementGraphRelation(GraphNode endNode, float lunghezzaArco, long elementoStrdaleDbId ){
    NodesRelationship relation = new NodesRelationship(this, endNode, lunghezzaArco, elementoStrdaleDbId);
    if( !versoCareggiataDoppio.contains(relation) ){
        versoCareggiataDoppio.add(relation);
    }
}

}

我的关系班:

@RelationshipEntity(type="ROAD_ELEMENT")
public class GraphNodesRelationship {
@GraphId
private Long relId;
//Id del record elemento stradale del DB
@Indexed(indexName="ELEMENTO_STRADALE_DB_ID", numeric=false, unique=true)
private String elementoStradaleDbId;
@StartNode
private GraphNode nodoIniziale;
@EndNode
private GraphNode nodoFinale;

//Peso dell'arco (lunghezza dell'elemento stradale)
private float lunghezzaArco;


public GraphNodesRelationship(GraphNode nodoIniziale, GraphNode nodoFinale, float lunghezzaArco, long elementoStradaleDbId){

    setNodoIniziale(nodoIniziale);
    setNodoFinale(nodoFinale);
    setLunghezzaArco(lunghezzaArco);
    setElementoStradaleDbId("elementoStradaleDbId_"+elementoStradaleDbId);
}
public GraphNodesRelationship(){

}
public GraphNode getNodoIniziale() {
    return nodoIniziale;
}

public void setNodoIniziale(GraphNode nodoIniziale) {
    Assert.notNull(nodoIniziale, "Impossibile creare la relazione; passato un nodoIniziale nullo"); 
    this.nodoIniziale = nodoIniziale;
}

public GraphNode getNodoFinale() {
    return nodoFinale;
}

public void setNodoFinale(GraphNode nodoFinale) {
    Assert.notNull(nodoFinale, "Impossibile creare la relazione; passato un nodoFinale nullo");
    this.nodoFinale = nodoFinale;
}

public float getLunghezzaArco() {
    return lunghezzaArco;
}

public void setLunghezzaArco(float lunghezzaArco) {
    this.lunghezzaArco = lunghezzaArco;
}
public String getElementoStradaleDbId() {
    return elementoStradaleDbId;
}
public void setElementoStradaleDbId(String elementoStradaleDbId) {
    this.elementoStradaleDbId = elementoStradaleDbId;
}   

}

如果我在尝试创建图表时以这种方式全部离开,并尝试保存关系,我会遇到这种错误:

org.springframework.dao.DataRetrievalFailureException: 0; nested exception is org.neo4j.graphdb.NotFoundException: 0
    at org.springframework.data.neo4j.support.Neo4jExceptionTranslator.translateExceptionIfPossible(Neo4jExceptionTranslator.java:63)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
    at com.sun.proxy.$Proxy25.save(Unknown Source)
    at it.eng.tz.pinf.graph.creator.service.impl.PinfDbSisGraphCreatorSvcImpl.creatGraph(PinfDbSisGraphCreatorSvcImpl.java:122)
    at it.eng.pinf.graph.extractor.test.PinfGraphTest.graphCreationTest(PinfGraphTest.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.neo4j.graphdb.NotFoundException: 0
    at org.neo4j.rest.graphdb.ExecutingRestAPI.getRelationshipById(ExecutingRestAPI.java:100)
    at org.neo4j.rest.graphdb.RestAPIFacade.getRelationshipById(RestAPIFacade.java:62)
    at org.neo4j.rest.graphdb.RestGraphDatabase.getRelationshipById(RestGraphDatabase.java:87)
    at org.springframework.data.neo4j.support.mapping.EntityStateHandler.getPersistentState(EntityStateHandler.java:110)
    at org.springframework.data.neo4j.support.mapping.EntityStateHandler.getPersistentState(EntityStateHandler.java:93)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.getPersistentState(Neo4jEntityPersister.java:221)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:248)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:231)
    at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:294)
    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.DefaultEntityState.setValue(DefaultEntityState.java:113)
    at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.setEntityStateValue(SourceStateTransmitter.java:70)
    at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.access$100(SourceStateTransmitter.java:40)
    at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter$4.doWithAssociation(SourceStateTransmitter.java:113)
    at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:269)
    at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:109)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:167)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:179)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:243)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:231)
    at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:294)
    at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:288)
    at org.springframework.data.neo4j.repository.AbstractGraphRepository.save(AbstractGraphRepository.java:113)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:358)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:343)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:155)
    ... 33 more

如果在我的Node类中我为关系设置了一个类型,那么一切都很好......这是否意味着我不能添加相同类型但不同方向的关系到节点?我应该为关系指定不同的类型吗?

我希望我很清楚

欢迎任何建议 谢谢

Angelo

0 个答案:

没有答案