@NodeEntity(label = "CompanyEntry")@Datapublic class CompanyEntryNode {@Idprivate String companyEntryId;/*** 模板id*/private String templateId;/*** 名称*/private String name;/*** 类型*/private String type;/*** 别名*/private String aliasName;}
生产关系类(ProductionRelationship)
@Data@RelationshipEntity(type = "Production")public class ProductionRelationship {@Idprivate String uuid;@StartNodeprivate CompanyEntryNode startNode;@EndNodeprivate ProductEntryNode endNode;/*** 创建关系词条的关系词条名称,例如:阿博茨公司 生产 产业链图谱*/private String relationEntryName;/*** 名称*/private String name;/*** 别名*/private String aliasName;/*** 简介*/private String introduction;}
在公司的dao层就直接返回CompanyEntryNode就行了
@Repositorypublic interface CompanyEntryRepository extends Neo4jRepository<CompanyEntryNode, String> {/*** 通过名称查询公司* @param name* @return*/@Query("match (c:CompanyEntry) where c.name={name} return c")CompanyEntryNode getEntryByName(String name);}
同时在生产关系的dao层直接返回ProductionRelationship,这里需要注意的是我在match查询时定义了一个productionRelationship 返回所有的查询结果,这样做的目的是把ProductionRelationshipg关系中的开始节点@StartNode和结束节点@EndNode也都返回了出来。
@Repositorypublic interface ProductionRelationshipRepository extends Neo4jRepository<ProductionRelationship, String> {*** 通过公司词条id和产品词条id 查询 ProductionRelationship** @param companyEntryId 公司词条id* @param productEntryId 产品词条id* @param relationName 关系类型名称* @return ProductionRelationship*/@Query("MATCH productionRelationship = (c:CompanyEntry)-[r]->(p:ProductEntry) " +"where type(r) = {relationName} " +"and c.companyEntryId={companyEntryId} " +"and p.productEntryId = {productEntryId}" +"return productionRelationship ")ProductionRelationship findProductionRelationship(String companyEntryId, String productEntryId, String relationName);}
查询生产关系的controller结果如下

这种结果包含了开始节点与结束节点的信息,如果不想要这些信息,只想定义一个Dto返回生产关系的属性信息。那该怎么做呢?这就用到了第二种接收方式-自定义类。这种定义方式就是只关心定义的信息。先在Dto定义一个类RelationshipDto ,这里要注意,我们用到了注解@QueryResult,他代表了Neo4j查询结果返回的类,这个注解不加的话会接收不到返回数据。
@Data@QueryResultpublic class RelationshipDto {/*** 关系uuid*/private String uuid;/*** 产品名称*/private String productName;/*** 产品uuid*/private String productEntryId;/*** 收入占比*/private String incomeProportion;}
接着我们到dao层定义返回一个集合结果List<RelationshipEntryDto> ,并且对应的值都要像 r.uuid as uuid这样单独的重命名下。这样自定义返回结果就OK了。
@Repositorypublic interface ProductionRelationshipRepository extends Neo4jRepository<ProductionRelationship, String> {*** 通过公司词条id和产品词条id 查询 ProductionRelationship** @param companyEntryId 公司词条id* @param productEntryId 产品词条id* @param relationName 关系类型名称* @return ProductionRelationship*/@Query("MATCH (c:CompanyEntry)-[r]->(p:ProductEntry) " +"where type(r) = {relationName} " +"and c.companyEntryId={companyEntryId} " +"and p.productEntryId = {productEntryId}" +"return r.uuid as uuid,p.name as productName,p.productEntryId as productEntryId")List<RelationshipEntryDto> findRelationship(String companyEntryId, String productEntryId, String relationName);}
我们controller层请求返回结果



下期文章开始我们谈谈Neo4j基于Lucene的搜索,以及用Neo4j做语义搜索等内容。- 本期完 -
,我会及时回复。由于微信限制了公众号留言功能,有问题你可以直接发公众号聊天,我会在下期末尾解答问题。为方便看最新内容,记得关注哦! 
文章转载自Neo4j权威指南,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




