# OGM save depth for relationship

**URL:** <https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496>\
**Category:** Spring Data Neo4j & Neo4j-OGM\
**Created:** [August 18, 2019, 5:10am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496 "2019-08-18T05:10:51Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![shayantest2](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shayantest2/32/5901_2.png) [@shayantest2](https://community.neo4j.com/u/shayantest2)\
**Post date:** [August 18, 2019, 5:10am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/1 "2019-08-18T05:10:51Z")

</div>

Consider the following code:

```auto
@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?

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [August 28, 2019, 11:26pm UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/2 "2019-08-28T23:26:40Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shayantest2](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shayantest2/32/5901_2.png) [@shayantest2](https://community.neo4j.com/u/shayantest2)\
**Post date:** [September 1, 2019, 4:49am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/3 "2019-09-01T04:49:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [September 2, 2019, 11:39pm UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/4 "2019-09-02T23:39:15Z")

</div>

It cascades the save.

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

---

<div class="post-metadata">

**Author:** ![shayantest2](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shayantest2/32/5901_2.png) [@shayantest2](https://community.neo4j.com/u/shayantest2)\
**Post date:** [September 3, 2019, 4:29am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/5 "2019-09-03T04:29:06Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![michael.hunger](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/michael.hunger/32/27377_2.png) [@michael.hunger](https://community.neo4j.com/u/michael.hunger)\
**Post date:** [September 3, 2019, 1:07pm UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/6 "2019-09-03T13:07:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![shayantest2](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shayantest2/32/5901_2.png) [@shayantest2](https://community.neo4j.com/u/shayantest2)\
**Post date:** [September 4, 2019, 6:45am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/7 "2019-09-04T06:45:45Z")

</div>

[here](https://neo4j.com/docs/ogm-manual/current/reference/) mentions that "A depth of 0 will persist only the properties of the specified entity to the database."

---

<div class="post-metadata">

**Author:** ![shayantest2](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/shayantest2/32/5901_2.png) [@shayantest2](https://community.neo4j.com/u/shayantest2)\
**Post date:** [September 4, 2019, 6:47am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/8 "2019-09-04T06:47:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![gerrit.meier](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gerrit.meier/32/27374_2.png) [@gerrit.meier](https://community.neo4j.com/u/gerrit.meier)\
**Post date:** [September 4, 2019, 3:13pm UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/9 "2019-09-04T15:13:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gerrit.meier](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gerrit.meier/32/27374_2.png) [@gerrit.meier](https://community.neo4j.com/u/gerrit.meier)\
**Post date:** [October 25, 2019, 5:58am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/10 "2019-10-25T05:58:28Z")

</div>

Split this topic on October 25, 2019, 5:58am UTC

A post was split to a new topic: [Hydrate related nodes before persisting a relationship](https://community.neo4j.com/t/hydrate-related-nodes-before-persisting-a-relationship/11324)

---

<div class="post-metadata">

**Author:** ![wurangbo](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/wurangbo/32/7933_2.png) [@wurangbo](https://community.neo4j.com/u/wurangbo)\
**Post date:** [December 10, 2019, 5:52am UTC](https://community.neo4j.com/t/ogm-save-depth-for-relationship/9496/11 "2019-12-10T05:52:14Z")

</div>

but how can we get relationship with custom query?
