Hi Max,

This is a case where the OGM attempts to infer the relationship mapping
because the object model does not correspond to the graph model.
When the relationship cannot be mapped in both directions, it maps
whichever direction it can and then the next thing it does is to try and
find collections that it might fit into. Since it cannot find a collection
either for that relationship type (ADDITIONAL_CHILD), it falls back to
finding a collection that accepts that particular node entity type i.e.
MyResource and writes this to the collection i.e. children. Since the OGM
does not enforce that you annotate everything (though we recommend it), it
tries its hardest to find some sort of mapping, which in your case, was
unexpected.

One way to prevent this happening is to have your object model resemble
your graph model so that the OGM does not have to start inferring things
(see http://graphaware.com/neo4j/2015/09/03/sdn-4-object-model.html). If
you add

@Relationship(type = "ADDITIONAL_CHILD", direction = Relationship.INCOMING)
private MyContainer container;

to MyResource (now you can navigate both ways via the ADDITIONAL_CHILD
relationship), you'll find that the entity loads as you expect.

The other way is for us to make the OGM a little smarter in cases like
this, and I've opened https://github.com/neo4j/neo4j-ogm/issues/38 to track
this.

Regards
Luanne


On Sat, Sep 5, 2015 at 11:14 PM, Max Spring <maximilian.spr...@gmail.com>
wrote:

> The following entities:
>
> @NodeEntity
> class MyEntity{
>     @GraphId Long graphId;
> }
>
> class MyResource extends MyEntity{
>     @Relationship(type="HAS_CHILD", direction = Relationship.INCOMING)
>     MyContainer parent;
> }
>
> class MyContainer extends MyResource{
>     @Relationship(type="HAS_CHILD", direction = Relationship.OUTGOING)
>     List<MyResource> children = new ArrayList<>();
>
>     MyResource additionalChild;
> }
>
> created this graph
>
> MyContainer{
>   additionalChild=MyResource{graphId=null, name='anotherChild'}
>   children=[
>     MyResource{graphId=null, name='child1'},
>     MyResource{graphId=null, name='child2'}
>   ]
> }
>
> loading it back gives:
>
> MyContainer{
>   additionalChild=MyResource{graphId=95, name='anotherChild'}
>   children=[
>     *MyResource{graphId=95, name='anotherChild'},*
>     MyResource{graphId=97, name='child2'},
>     MyResource{graphId=96, name='child1'}
>   ]
> }
>
> Somehow additionalChild bleeds over into the children collection.
>
> Full example is on Github
> <https://github.com/m2spring/ogm-eval/tree/single-child-in-children>.
> -Max
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Neo4j" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to neo4j+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to neo4j+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to