Spring Data Neo4j @RelatedToVia error on save()
I'm getting the following error when I try to save an entity which has a
@RelatedToVia attribute:
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]
From the error description above it seems that my RelationshipEntity is
missing the end node. However, and this is the worst part of the problem,
this is not true because I get this error randomly.
Here is the scenario. I'm creating some really simple tests just to check
my class mappings. I manually create the classes necessary for each test
case and then I save them. As Spring Data "cascades" the persistence of
the entities my only concern is to populate the entity under test with its
primitive properties and related entities, save it and then retrieve it
back to see if the data is there.
This worked well for my first few classes which do not have @RelatedToVia
mappings, but not for the ones which use @RelatedToVia. Here are some
excerpts from the code which uses @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;
}
Plus, here is an image of the variables state in debbuging mode just
before calling the BasicProbabilityAssignmentFunction repository save.
Notice that the Belief entity is fully populated! (please get the image
here: http://i.stack.imgur.com/FeWm7.png. I don't know why, but
stackoverflow requires reputation to post images. It makes no sense to
me.)
And also the code used for test:
//this just creates an instance with its attributes populated
BasicProbabilityAssignmentFunction bpaFunction =
BasicMockFactory.createBpaFunction();
//this is where I get the error.
bpaFunction = bpaFunctionRepository.save(bpaFunction);
One further note! I managed to stop getting this error by saving all
entities (e.g., Proposition, Hypothesis etc) related to
BasicProbabilityAssignmentFunction before saving
BasicProbabilityAssignmentFunction itself. Nevertheless, I'm not sure why
this solved the problem.
No comments:
Post a Comment