首先我们要知道在图中关系是有关系指向的
比如我们有如下实体
user : 用户 , shop : 商店 , sku : 商品
我们想让他们三者具有如下的关系
user----buy-->sku <-----own---- shop
在图数据库中显示如下
那么我们在代码中要怎样构建这样的实体关系呢 ?
(我的domain :基本目录结构如下)
一、首先我们先定义节点和关系的公共基类
节点和关系的基类
package org.jsg.domain.base;import org.neo4j.ogm.annotation.GraphId;/** * 最基础的entity无论是节点还是关系都需要继承 * 提供最基础的 id属性,equals、hashCode方法 * * @author zjl */public abstract class AllBaseEntity { /** * id 必须为Long类型,而且必须提供(节点和关系都需要)。且要加这个注解。 * id 由图数据库统一操作,所以不需要setter */ @GraphId private Long id; public Long getId() { return id; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } if (getClass() != obj.getClass()) { return false; } if (this.id == null) { // For newly created entity, id will be null return false; } AllBaseEntity entity = (AllBaseEntity) obj; return this.id.equals(entity.id); } @Override public int hashCode() { return id == null ? super.hashCode() : id.hashCode(); }}
节点的基类
package org.jsg.domain.base;/** * 节点需要继承的基础类 * 提供节点的公共属性-name * * @author zjl */public abstract class NodeBaseEntity extends AllBaseEntity { /** * 在图数据库中每个节点展示的名字 */ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; }}
二、定义我们的节点
package org.jsg.domain;import org.jsg.domain.base.NodeBaseEntity;import org.neo4j.ogm.annotation.NodeEntity;import org.neo4j.ogm.annotation.Relationship;import java.util.List;/** * 节点-店铺 * * @author zjl */@NodeEntitypublic class shop extends NodeBaseEntity {// 之所以类名小写是因为SpringDate-neo4j 生成的节点标识和类名相同,为了匹配数据给过来的数据,所以小写了类名 /** * 一个属性值:店铺名称 */ private String shop_name; /** * 商店有那些商品 * 关系指向在定义时已经确定shop ->sku */ @Relationship(type="own", direction=Relationship.INCOMING) private Listskuses; public String getShop_name() { return shop_name; } public void setShop_name(String shop_name) { this.shop_name = shop_name; } public List getSkuses() { return skuses; } public void setSkuses(List skuses) { this.skuses = skuses; }}
package org.jsg.domain;import org.jsg.domain.base.NodeBaseEntity;import org.neo4j.ogm.annotation.NodeEntity;import org.neo4j.ogm.annotation.Relationship;import java.util.List;/** * 节点-商品SKU * * @author zjl */@NodeEntitypublic class sku extends NodeBaseEntity {// 之所以类名小写是因为SpringDate-neo4j 生成的节点标识和类名相同,为了匹配数据给过来的数据,所以小写了类名 /** * 商品的SKUId */ private String item_sku_id; /** * 商品质量标志(违规商品-正常商品) */ private Integer bad_sku_flag; /** * 商品名称 */ private String sku_name; /** * 被统计次数 */ private Integer possible_bad_sku_times; /** * 商品被那些用户购买了 * 关系指向在定义时已经确定:user -> sku */ @Relationship(type="buy", direction=Relationship.INCOMING) private Listusers; /** * 那些店铺拥有此商品 * 关系指向在定义时已经确定:shop ->sku */ @Relationship(type="own", direction=Relationship.INCOMING) private List shops; public List getShops() { return shops; } public void setShops(List shops) { this.shops = shops; } public String getItem_sku_id() { return item_sku_id; } public void setItem_sku_id(String item_sku_id) { this.item_sku_id = item_sku_id; } public Integer getBad_sku_flag() { return bad_sku_flag; } public void setBad_sku_flag(Integer bad_sku_flag) { this.bad_sku_flag = bad_sku_flag; } public String getSku_name() { return sku_name; } public void setSku_name(String sku_name) { this.sku_name = sku_name; } public List getUsers() { return users; } public void setUsers(List users) { this.users = users; } public Integer getPossible_bad_sku_times() { return possible_bad_sku_times; } public void setPossible_bad_sku_times(Integer possible_bad_sku_times) { this.possible_bad_sku_times = possible_bad_sku_times; }}
package org.jsg.domain;import org.jsg.domain.base.NodeBaseEntity;import org.neo4j.ogm.annotation.NodeEntity;import org.neo4j.ogm.annotation.Relationship;import java.util.List;/** * 节点-用户 * * @author zjl */@NodeEntitypublic class user extends NodeBaseEntity {// 之所以类名小写是因为SpringDate-neo4j 生成的节点标识和类名相同,为了匹配数据给过来的数据,所以小写了类名 /** * 用户编号 */ private String user_id; /** * 用户购买了那些商品 * 关系指向在定义时已经确定:user -> sku */ @Relationship(type="own", direction=Relationship.INCOMING) private Listskuses; public String getUser_id() { return user_id; } public void setUser_id(String user_id) { this.user_id = user_id; } public List getSkuses() { return skuses; } public void setSkuses(List skuses) { this.skuses = skuses; }}
三、定义关系
package org.jsg.domain.relationship;import org.jsg.domain.base.AllBaseEntity;import org.neo4j.ogm.annotation.EndNode;import org.neo4j.ogm.annotation.RelationshipEntity;import org.neo4j.ogm.annotation.StartNode;/** * 关系-买 * 关系的指向为StartNode 指向EndNode(StartNode -> EndNode : user -> sku) 在定义关系时已经确定 * * @autor zjl */@RelationshipEntity(type="buy") // 此处注解中type的值就是数据库中关系存储的name值public class Buy extends AllBaseEntity { @StartNode private org.jsg.domain.user user; @EndNode private org.jsg.domain.sku sku; public org.jsg.domain.user getUser() { return user; } public void setUser(org.jsg.domain.user user) { this.user = user; } public org.jsg.domain.sku getSku() { return sku; } public void setSku(org.jsg.domain.sku sku) { this.sku = sku; }}
package org.jsg.domain.relationship;import org.jsg.domain.shop;import org.jsg.domain.base.AllBaseEntity;import org.neo4j.ogm.annotation.EndNode;import org.neo4j.ogm.annotation.RelationshipEntity;import org.neo4j.ogm.annotation.StartNode;/** * 关系-拥有 * 关系的指向为StartNode 指向EndNode (StartNode -> EndNode : shop ->sku) 在定义关系时已经确定 * * @autor zjl */@RelationshipEntity(type="own") // 此处注解中type的值就是数据库中关系存储的name值public class Own extends AllBaseEntity { @StartNode private shop shop; @EndNode private org.jsg.domain.sku sku; public shop getShop() { return shop; } public void setShop(shop shop) { this.shop = shop; } public org.jsg.domain.sku getSku() { return sku; } public void setSku(org.jsg.domain.sku sku) { this.sku = sku; }}