Unique index on a superclass and value reusable between different subclasses
In a project using Spring Data Neo4j 3.2, I have a model that looks like this:
@NodeEntity
public abstract class A {
@GraphId
public Long id;
@Indexed(unique = true)
public Long technicalId;
}
@NodeEntity
public class B extends A {}
@NodeEntity
public class C extends A {}
I missed the paragraph in the documentation about the default merge function and we ran into the following situation without getting an exception:
B b = new B();
b.technicalId = 42;
neo4jTemplate.save(b);
// I now have Node[1] in Neo4j with the following labels: B, _B, A
C c = new C();
c.technicalId = 42;
neo4jTemplate.save(c);
// No exception and Node[1] now has the following labels: B, _B, A, C, _C
So, the situation can be avoided:
-
either using instance-based indexes :
@NodeEntity public abstract class A { @GraphId public Long id; @Indexed(unique = true, level = Level.INSTANCE) public Long technicalId; }
-
or using
failOnDuplicate
(although it is not mentioned in the documentation, even for 3.3, but I found it here and here ), which avoids creating 3 indexes instead of 1, so less I / O on write and less disk space:@NodeEntity public abstract class A { @GraphId public Long id; @Indexed(unique = true, failOnDuplicate = true) public Long technicalId; }
However, is it not a mistake that the default behavior will change the nature of the node (given a class hierarchy) and create a combination of labels that might prevent the object from being created? I mean the node now has two labels prefixed with:, n:B:_B:A:C:_C
because two Cypher requests were requested:
MERGE (n:`A` {`technicalId`: {value}})
ON CREATE SET n={props} SET n:SupplierCompany
return n
// params {value=42, props={technicalId=42}}
match (n)
where id(n)={nodeId}
set n:`A`:`C`:`_C`
// params {nodeId=1}
Should I be raising a test case issue on SDN JIRA or github ?
source to share
No one has answered this question yet
See similar questions:
or similar: