OGM save depth for relationship

Consider the following code:

@NodeEntity
public class Entity {
	...
}

@RelationshipEntity
public class RelationInstance{
	@StartNode
	Entity src;
	@EndNode
	Entity dst;
	...
}


Entity en1 = new Entity();
session.save(en1);
Entity en2 = new Entity();	// en2 is not saved.
session.save(new RelationInstance(en1, en2), 0)

I expect that the code throws an exception because en2 is not persisted and we call the second save with a depth parameter of 0. But it seems that it does not throw an exception and saves en2. Why is that? How should I make it throw an exception?

1 Like

Your code cannot compile, you cannot call the two classes the same.
Can you try to save en2 first.

You are also missing a relationship-type in the annotation.

It was a mistake. I changed the second class name. However, my question still exists.

It cascades the save.

If you want an exception you need to check for nulls in your relationship-entity in a pre-save.

So what is the purpose of the depth parameter? Shouldn't the "save" not save the related objects when a depth of 0 is mentioned?

Ah sorry, I missed that, good question. I think 0 is infinite. And 1 is one level.

But I'd leave that to @gerrit.meier who's more knowledgable than me on this.

here mentions that "A depth of 0 will persist only the properties of the specified entity to the database."

Also, it seems that not only it saves the entity if it does not exist, but also it updates it if it previously exists. Seems to ignore the depth!

For relationship-centric saves the relationship and the nodes will get persisted.
This is needed because the RelationshipEntity does not get identified just by its own id but also the nodes that the relationship connects.

You are right, the documentation is a little bit vague and should say NodeEntity instead of just entity.

1 Like

A post was split to a new topic: Hydrate related nodes before persisting a relationship

but how can we get relationship with custom query?