How do I add a second label to a node in Spring Data Neo4J 3.0.0 (Release)?

こ雲淡風輕ζ 提交于 2019-12-12 15:36:31

问题


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:

  1. Reference the current node in the query.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!