SND - entity creation

There are two entities: node entity and relationship entity.
 1.@NodeEntity
2.@RelationshipEntity

1. Node entity

package org.canaan.neo4j.graph.entity;

import lombok.Data;
import org.canaan.neo4j.graph.convert.MoneyConverter;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.Labels;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.typeconversion.Convert;
import org.neo4j.ogm.annotation.typeconversion.DateLong;

import java.util.Date;
import java.util.Set;

/**
 * {@link @NodeEntity}  label Do not specify the default as class name i.e. UserNode
 *
 * @author Canaan
 * @date 2019/7/11 8:52
 */
@Data
@NodeEntity(label = "User")
public class UserNode {

    public final static String LABEL_FOUNDER = "Founder";
    public final static String LABEL_PLOUGH  = "Plough";
    public final static String LABEL_SONUZ   = "Sonuz";

    /**
     * {@link @Id} Business primary key, which can only be generated by {@ link @GeneratedValue}
     *
     * @author Canaan
     * @date 2019/7/11 12:47
     */
    @Id
    private Long userId;


    //@Required requires enterprise level neo4j
    private String userName;


    @DateLong   //Convert date type to timestamp or @ DateString to String
    private Date createTime;

    @Convert(MoneyConverter.class)
    private Long fundValue;

    /**
     * Append additional label after @ nodeentity label
     *
     * @author Canaan
     * @date 2019/7/11 11:56
     */
    @Labels
    private Set<String> labels;

    /**
     * The default value for specifying a relationship is [-- >]
     *
     * @author Canaan
     * @date 2019/7/11 14:31
     */
    @Relationship(type = "SUPERIOR")
    private UserNode parentNode;


    //@PostLoad
    //public void loadData() {
    //When the data is loaded, this method is called
    //    System.out.println("data loaded");
    //}


}

Custom type converter:

package org.canaan.neo4j.graph.convert;

import org.neo4j.ogm.typeconversion.AttributeConverter;

/**
 * @author Canaan
 * @date 2019/7/11 19:06
 */
public class MoneyConverter implements AttributeConverter<String, Integer> {

    @Override
    public Integer toGraphProperty(String value) {
        return Integer.valueOf(value);
    }

    @Override
    public String toEntityAttribute(Integer value) {
        return value.toString();
    }


}

The main annotations of the entity are under the package org.neo4j.ogm.annotation.
The annotation of the field type converter is under the package org.neo4j.ogm.annotation.typeconversion.

Be careful:

1. If the field name is id and there is no annotation, the default return is GraphId.
2. The default value of NodeEntity type is the class name.
3.@Id Is business, not generated by neo4j
 

File:

https://docs.spring.io/spring-data/neo4j/docs/5.1.3.RELEASE/reference/html/#reference:annotating-entities:node-entity

Description of entity Id:
https://docs.spring.io/spring-data/neo4j/docs/5.1.3.RELEASE/reference/html/#reference:annotating-entities:graph-id

 

2. Relation entity, mainly used for complex relation mapping

package org.canaan.neo4j.graph.entity;

import lombok.Data;
import org.neo4j.ogm.annotation.*;

/**
 * User affiliation
 *
 * @author Canaan
 * @date 2019/7/11 10:10
 */
@Data
@RelationshipEntity(type = Membership.TYPE)
public class Membership {

    public final static String TYPE = "MEMBERSHIP";

    @Id
    @GeneratedValue
    private Long relationshipId;

    @StartNode
    private UserNode startNode;

    @EndNode
    private UserNode endNode;

    public Membership() {
    }

    public Membership(UserNode startNode, UserNode endNode) {
        this.startNode = startNode;
        this.endNode = endNode;
    }
}

 

Note that if the relationship mapping is not correct, you cannot cascade query its relationship nodes.

 

File:

https://docs.spring.io/spring-data/neo4j/docs/5.1.3.RELEASE/reference/html/#reference:annotating-entities:relationship-entity

Keywords: Database Spring Lombok Java

Added by NightCoder on Wed, 30 Oct 2019 22:52:50 +0200