项目需求使用java对neo4j图谱进行管理。自己使用springboot整合neo4j,并实现节点和关系管理的demo。
需求说明: 节点有entity,content,param等类型,但是要求所有节点必须有node类型,也就是说有两个labels属性。
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-http-driver</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.yml
spring:
data:
neo4j:
uri: http://127.0.0.1:7474
username: neo4j
password: pactera
使用@labels注解实现
@labels注解在spring-data-neo4j文档上找到的(https://docs.spring.io/spring-data/data-neo4j/docs/current/reference/html/)。这样做可以在创建节点时添加任意lables。
@NodeEntity(label="entity")
@Data
public class EntityNode {
public EntityNode() {
List<String> labels = new ArrayList<>();
labels.add("node");
labels.add("entity");
this.setLabels(labels);
}
@GeneratedValue
@Id
private Long id;
@Labels
private List<String> labels ;
public String nodeId;
public String nodeName;
private String[] concepts;
private String regex;
// 关系映射
@Property(name="default")
private String defaultJump;
}
// 继承Neo4jRepository
public interface EntityRepository extends Neo4jRepository<EntityNode, Long> {
EntityNode findByNodeId(@Param("nodeId") String NodeId);
}
这样做易于理解和实现,但是却存在很多问题:因为@NodeEntity中label="entity",所以EntityRepository 中实现的函数全部都是针对entity类型的节点的。比如nodeRepository.findAll()函数对应执行语句为:"statement":"MATCH (n:`entity`) RETURN n"。那么如果给你一个id,你不知道具体类型,那么这时该使用entityRepository,contentRepository还是paramRepository来findById呢?
使用继承
@NodeEntity注解源码
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface NodeEntity {
String LABEL = "label";
String label() default "";
}
当时我也被这个问题困惑很久,最后发现@NodeEntity注解上有@Inherited注解,想到是否可以使用继承?
父类node,定义节点的公共属性。(自己代码中父类没有使用@Data,而是写的get,set方法)
@NodeEntity(label="node")
@Data
public class Node implements Serializable{
private static final long serialVersionUID = 942659633583923942L;
@GeneratedValue
@Id
private Long id;
public String nodeId;
public String nodeName;
}
public interface NodeRepository extends Neo4jRepository<Node, Long> {
}
子类,具体类的具体属性
@NodeEntity(label="entity")
@Data
@EqualsAndHashCode(callSuper=false)
public class EntityNode extends Node {
private static final long serialVersionUID = -3441407202518609463L;
private String[] concepts;
private String regex;
@Property(name="default")
private String defaultJump;
}
public interface EntityRepository extends Neo4jRepository<EntityNode, Long> {
EntityNode findByNodeId(@Param("nodeId") String NodeId);
}
使用EntityRepository 创建entity类型节点
@Test
@Rollback(false)
public void createNode() {
EntityNode entityNode= new EntityNode();
entityNode.setNodeId("test001");
entityNode.setNodeName("测试1");
entityNode.setRegex("123123123131");
entityRepository.save(entityNode);
}
// 执行语句
{"statement":"UNWIND {rows} as row CREATE (n:`entity`:`node`) SET n=row.props RETURN row.nodeRef as ref, ID(n) as id, {type} as type
使用nodeRepository删除任意类型的节点
@Test
@Rollback(false)
public void deleteById() {
nodeRepository.deleteById(1488l);
}
// 执行语句
{"statement":"MATCH (n:`node`) WHERE ID(n) = { id } WITH n RETURN n"}
{"statement":"MATCH (n) WHERE ID(n) = { id } OPIONAL MATCH (n)-[r0]-() DELETE r0, n"}