Jon Worek
Jon Worek

Reputation: 351

Why does Spring Data Neo4j create an underscore prefixed label on nodes?

I realize that a similar question was asked here, but I'm trying to understand the reasoning behind the approach of SDN to create a label matching my class name, and also creating the same label prefixed by an underscore.

So, for example, I have a Patient class. When I create my @NodeEntity decorated Patient class through my Neo4j repository and then query it back through the Neo4j web console, I see Patient and _Patient as the labels.

As an extension to this question, say I have the following inheritance hierarchy of classes representing nodes:

@NodeEntity
public class Patient extends Person {
   //class definition here
}

@NodeEntity
public abstract class Person {
    //class definition here
}

When I save an instance of Patient to the database, it will have three labels: Person, Patient, _Patient. Why wouldn't my node also have an _Patient label?

Upvotes: 2

Views: 446

Answers (1)

František Hartman
František Hartman

Reputation: 15086

When your hierarchy has more than 1 (not abstract) classes the label prefixed with _ allows SDN to determine the type properly.

E.g. when you similar hierarchy

Person
Patient extends Person
EbolaPatient extends Patient

then let's say you save instance of class Patient then the node will have _Patient label, when you save EbolaPatient instance it will have _EbolaPatient label.

If you then retrieve the nodes (e.g. as a collection using findAll on a person repository) SDN will correctly instantiate the entities as Patient and EbolaPatient.

Other option how to implement this would be to find a label that is most down the class hierarchy, which is a lot more complicated than having additional prefixed label.

To see details how this is implemented see LabelBasedNodeTypeRepresentationStrategy class in SDN project.

Upvotes: 4

Related Questions