3.10. Domain entities

This section describes one way to handle domain entities when using Neo4j.

Entities can be wrapped around a node. The same approach can be used with relationships.

The source code of the examples is found here: Person.java

First off, store the node and make it accessible inside the package:

    private final Node underlyingNode;

    Person( Node personNode )
    {
        this.underlyingNode = personNode;
    }

    protected Node getUnderlyingNode()
    {
        return underlyingNode;
    }

Delegate attributes to the node:

    public String getName()
    {
        return (String)underlyingNode.getProperty( NAME );
    }

Make sure to override these methods:

    @Override
    public int hashCode()
    {
        return underlyingNode.hashCode();
    }

    @Override
    public boolean equals( Object o )
    {
        return o instanceof Person &&
                underlyingNode.equals( ( (Person)o ).getUnderlyingNode() );
    }

    @Override
    public String toString()
    {
        return "Person[" + getName() + "]";
    }