【问题标题】:Spring Data Neo4j @RelatedToVia error on save()Spring Data Neo4j @RelatedToVia 保存错误()
【发布时间】:2013-08-23 03:07:13
【问题描述】:

当我尝试保存具有@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 Data “级联”实体的持久性,我唯一关心的是使用其原始属性和相关实体填充被测实体,保存它然后检索它以查看数据是否存在。

这对我最初的几个没有@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 存储库保存之前调试模式下的变量状态图像。请注意,信念实体已完全填充!

还有用于测试的代码:

//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 相关的所有实体(例如,命题、假设等)来停止出现此错误。不过,我不确定为什么这解决了问题。

回答迈克尔评论:迈克尔,你是说 rel-type 应该在 Belief 类本身中定义(而不是使用 @RelatedToVia 注释的 type 属性),否则我应该使用模板.createRelationshipBetween?我尝试使用 @RelationshipEntity 类型属性,但问题仍然存在。起作用的是在@Startnode(BasicProbabilityAssignmentFunction)之前保存关系@EndNode(Proposition)。通过这样做,当 BasicProbabilityAssignmentFunction 被保存时,信念关系被毫无问题地创建(保存)。

【问题讨论】:

  • 还要确保如果您添加 Beliefs,则它们的所有 3 个字段(开始节点、结束节点和 rel 类型)都已填充。否则,您也可以直接保存关系实体或使用template.createRelationshipBetween()

标签: spring-data-neo4j


【解决方案1】:

【讨论】:

  • 来自https://jira.springsource.org/browse/DATAGRAPH-216 看来这个问题已经在我正在使用的2.2版中解决了。另外,您可能会注意到,错误是随机出现的。我假设这可能与保存实体的顺序有关。看起来有时弹簧数据没有选择正确的顺序。不过,不确定。
  • 一般来说,最好还是直接保存关系实体,然后创建关系,并在下一次加载实体时可用。
猜你喜欢
  • 1970-01-01
  • 2023-02-23
  • 1970-01-01
  • 1970-01-01
  • 2015-10-09
  • 2023-01-30
  • 1970-01-01
  • 2017-10-11
  • 1970-01-01
相关资源
最近更新 更多