Muhammad Gelbana
Muhammad Gelbana

Reputation: 4000

How to map a Neo4j node using embeddable objects?

I'm asking if this is doable in Spring-Data-Neo4j (SDN) or Neo4j OGM because I understand that SDN actually uses Neo4j-OGM underneath.

Assume I have 2 Java objects that I need to map to a single Graph node:

@NodeEntity
public class User {
    @GraphId
    private Long id;
    private ComplexInfo info;
}
@NodeEntity
public class ComplexInfo {
    @GraphId
    private Long id;
    private Long age;
    private String name;
}

This way, I'll have a relation between 2 nodes. User and ComplexInfo.

But is there a way to map this as a single node, where the primitive variables (including String and wrapper Objects such as Long, Integer..etc) of the ComplexInfo java object would be persisted within the User node and there would be no existence of the ComplexInfo node ?

Effectively it would be as if I have mapped my User object this way:

@NodeEntity
public class User {
    @GraphId
    private Long id;
    private Long age;
    private String name;
}

I don't want to have 2 nodes for this because the ComplexInfo class is no more than a collection of reusable properties and there is no benefit of having a relationship between it and the node having this properties.

Upvotes: 2

Views: 487

Answers (1)

digx1
digx1

Reputation: 1088

I'm assuming here that you are after embedding the ComplexInfo class into the User class. To be clear this means that ComplexInfo will not appear in the database as a node and cannot be directly looked up through the OGM; it must always be accessed through composition of an annotated domain object.

As @troig mentions the only way to do this is to upgrade to the latest snapshot version of the OGM (2.1 GA will be out around 12th December). You can then follow Jasper's example here. It should be pretty easy to work with your domain. Just remember to remove the @NodeEntity annotation and the @GraphId Long id field in ComplexInfo.

Upvotes: 2

Related Questions