Search code examples
neo4j-ogm

Saving Entity does not update class-hierachy labels correctly


I have an abstract superclass Report and two Subclasses SimpleReport and ExtendedReport, which I want to persist in my database.

If a SimpleReport is created, it has the labels "Report" and "SimpleReport" attached to it, as expected.

A user can modify such a SimpleReport, which leads to the SimpleReport becoming an ExtendedReport.

If I now save this ExtendedReport (using the same ID as the SimpleReport, because I just want to update it) it has the labels "Report", "SimpleReport"and "ExtendedReport" attached to it.

IMHO the label "SimpleReport" should be removed on save. I`m currently deleting the wrong label using a cypher query after saving the updated report.

I´m asking if there is a better way to archive this, if may approach is wrong or if this is a bug in ogm?


Solution

  • The rules for labels are as follows:

    • any plain concrete class in the hierarchy generates a label by default
    • plain abstract class does not generate a label by default
    • plain interface does not generate a label by default
    • any class annotated with @NodeEntity or @NodeEntity(label="something") generates a label
    • empty or null labels must not be allowed
    • classes / hierarchies that are not to be persisted must be annotated with @Transient

    Therefore if you remove abstract from your base class, or add a @NodeEntity annotation, you will see the results you expect.

    Edit:

    The OGM does not remove labels when a class is renamed. Any additional labels are left intact.

    • You can remove these manually using direct database access.
    • You can declare a field with the @Labels annotation to manage adding/removing additional labels from an entity.