SND - 實體建立

 

實體主要有兩個,一個是【節點實體】 一個是【關係實體】
 1.@NodeEntity
2.@RelationshipEntityhtml

1.節點實體java

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 不指定默認爲 類名即: 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} 業務主鍵,惟一 可使用 {@link @GeneratedValue} 生成
     *
     * @author Canaan
     * @date 2019/7/11 12:47
     */
    @Id
    private Long userId;


    //@Required 須要企業級neo4j
    private String userName;


    @DateLong   //將日期類型轉換爲時間戳,或者用 @DateString 轉換爲String
    private Date createTime;

    @Convert(MoneyConverter.class)
    private Long fundValue;

    /**
     * 在 @NodeEntity  label 後追加 額外的標籤
     *
     * @author Canaan
     * @date 2019/7/11 11:56
     */
    @Labels
    private Set<String> labels;

    /**
     * 關係的指定默認是   【-->】
     *
     * @author Canaan
     * @date 2019/7/11 14:31
     */
    @Relationship(type = "SUPERIOR")
    private UserNode parentNode;


    //@PostLoad
    //public void loadData() {
    //當數據加載完成後,將調用該方法
    //    System.out.println("數據加載了");
    //}


}

自定義類型轉換器: node

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();
    }


}

實體的主要註解在:   org.neo4j.ogm.annotation 包下
字段類型轉換器註解在:  org.neo4j.ogm.annotation.typeconversion 包下spring

注意:ide

1. 若是字段名爲 【id】 面沒有註解說明,那麼默認返回的是 GraphId
2. NodeEntity type 的默認值爲 類名 。
3.@Id 是業務上的,不是由neo4j 生成 

 ui

文檔:this

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

實體Id 說明:
https://docs.spring.io/spring-data/neo4j/docs/5.1.3.RELEASE/reference/html/#reference:annotating-entities:graph-id.net

 

 2. 關係實體, 主要用於複雜的關係映射code

package org.canaan.neo4j.graph.entity;

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

/**
 * 用戶隸屬關係
 *
 * @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;
    }
}

 

注意若是關係映射不正確,是沒法級聯查詢其關係節點的

 

文檔:

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

相關文章
相關標籤/搜索