我收到以下错误,当我尝试保存具有@RelatedToVia属性的实体:上的保存()春季数据的Neo4j @RelatedToVia错误
org.springframework.dao.InvalidDataAccessApiUsageException: Null parameter, startNode=NodeImpl#1, endNode=null, type=DynamicRelationshipType[BPA_PROPOSITION]; nested exception is java.lang.IllegalArgumentException: Null parameter, startNode=NodeImpl#1, endNode=null, type=DynamicRelationshipType[BPA_PROPOSITION]
从错误的描述上面看来,我的RelationshipEntity缺少末端节点。然而,这是问题中最糟糕的部分,这是不正确的,因为我随机得到这个错误。
这是场景。我创建了一些非常简单的测试来检查我的类映射。我手动创建每个测试用例所需的类,然后保存它们。由于Spring数据“级联”了实体的持久性,我唯一担心的是使用它的基本属性和相关实体来填充被测实体,保存并重新获取它以查看数据是否存在。
这对我的前几个类没有@RelatedToVia映射,但对于使用@RelatedToVia的类没有效果。以下是使用@RelatedToVia的代码摘录。
@NodeEntity
public class BasicProbabilityAssignmentFunction {
@GraphId
private Long id;
@RelatedTo(type = RelTypeConstants.BPA_FRAME, direction = Direction.OUTGOING)
private FrameOfDiscernment frameOfDiscernment;
@RelatedToVia(type = RelTypeConstants.BPA_PROPOSITION, direction = Direction.OUTGOING, elementClass = Belief.class)
private Set<Belief> beliefs;
}
@RelationshipEntity
public class Belief {
@GraphId
private Long id;
@StartNode
private BasicProbabilityAssignmentFunction bpaFunction;
@EndNode
private Proposition proposition;
}
@NodeEntity
public class Proposition {
@GraphId
private Long id;
@RelatedTo(type= RelTypeConstants.PROPOSITION_HYPOTHESIS, direction= Direction.OUTGOING)
private Set<Hypothesis> hypotheses;
@RelatedTo(type = RelTypeConstants.FRAME_PROPOSITION, direction = Direction.INCOMING)
private FrameOfDiscernment frameOfDiscernment;
}
另外,这里只是调用BasicProbabilityAssignmentFunction库保存之前debbuging模式下的变量状态的图像。请注意,信仰实体已经完全填充!
,也可用于测试代码:
//this just creates an instance with its attributes populated
BasicProbabilityAssignmentFunction bpaFunction = BasicMockFactory.createBpaFunction();
//this is where I get the error.
bpaFunction = bpaFunctionRepository.save(bpaFunction);
一个进一步的音符!通过在保存BasicProbabilityAssignmentFunction本身之前保存与BasicProbabilityAssignmentFunction相关的所有实体(例如Proposition,Hypothesis等),我设法停止了这个错误。尽管如此,我不知道为什么这解决了这个问题。
接听迈克尔评论:迈克尔,你说,REL-类型应该在信仰类本身(而不是使用@RelatedToVia注释的type属性)来定义或以其他方式我应该使用template.createRelationshipBetween?我试图使用@RelationshipEntity类型属性,但问题仍然存在。有用的是在@Startnode(BasicProbabilityAssignmentFunction)之前保存关系@EndNode(Proposition)。通过这样做,当BasicProbabilityAssignmentFunction被保存时,信念关系被创建(保存)而没有问题。
另外,还要确保如果添加'Beliefs',他们的所有3场(开始节点,终端节点和rel类型)被填充。否则,您也可能直接保存关系实体或使用'template.createRelationshipBetween()' –