问题
In Neo4J I have a @NodeEntity Person
.
I'd like to be able to also add additional labels such as :USER
, :CUSTOMER
, :OWNER
, :AGENT
, etc.
It seems that spring-data-neo4j:3.0.0-RELEASE
has support for an @Labels
annotation, but I get a NullPointerException
when trying to use it.
@NodeEntity
public class Person {
@GraphId
Long id
@Indexed(unique=true)
String email
@Labels // <- Seems this is unsupported.
private Collection<String>labels
public void addLabel(String label) {
this.labels.add(label) // <- NullPointer thrown here.
}
}
I assume this is because it's not yet supported. If this is indeed the case, would someone kindly give me an example of how to achieve the same result by updating the repository behind it, adding a manual @Query
annotation to add the label?
I'm not sure how to:
- Reference the current node in the query.
- Execute the cypher after the save() method has been called and the node has been created.
回答1:
If you remodel your domain objects to support inheritance, SDN will derive additional labels based on inheritance tree.
if you want more than one label, extend the parent classes and you will have the desired label.
For example, if
@NodeEntity
public class User extends Customer {
}
Will generate two labels :User and :Customer.
See Use @NodeEntity on interface/abstract class on using abstract classes with Neo4j.
来源:https://stackoverflow.com/questions/29769749/how-do-i-add-a-second-label-to-a-node-in-spring-data-neo4j-3-0-0-release