当前位置:   article > 正文

springboot深入实践-2.4Neo4j数据库详解_spring-data-neo4j relation

spring-data-neo4j relation

2.4 Neo4j

Neo4j既具有传统关系型数据库的优点,又具有NoSQL数据库优势的一种数据库.

Neo4j将结构化数据存储在一张图上,图中每一个节点的属性表示数据的内容,每一条有向边表示数据的关系。Neo4j没有表结构的概念,它的数据用节点的属性来表示。

2.4.1 Neo4j依赖配置

直接在pom中导入依赖即可。

   <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
   </dependency>
  • 1
  • 2
  • 3
  • 4

2.4.2 节点和关系实体建模

虽然Neo4j没有表结构的概念,但是它有节点和关系的概念。例如,现在有演员和电影两个实体,它们的关系表现为一个演员在一部电影中扮演一个角色。

那么就可以创建演员和电影两个节点实体,和一个角色关系实体。

image-20190417150337135

这个实体-关系模型是可以不经过任何转换而直接存入数据库的,也就是说,在Neo4j图数据库中保存的数据与上图所示的相同,它仍然是一张图。这对于业务人员和数据库设计人员来说,它的意义相同。所以使用Neo4j数据库,将在很大程度上减轻了设计工作和沟通成本。

JPA使用了ORM,Neo4j使用了对象图形映射(Object-Graph Mapping,OGM)对象图形映射的方式来建模。

Actor

@NodeEntity //标志这个类是一个节点实体
public class Actor {
    @Id
    @GeneratedValue
    Long id;
    private String name;
    private int born;

    public Actor() { }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setBorn(int born) {
        this.born = born;
    }

    public int getBorn() {
        return born;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

Movie

@NodeEntity
public class Movie {
    @Id
    @GeneratedValue
    Long id; // 表明id是主键,且是自动生成的。
    String title;
    String year;

    @Relationship(type="角色", direction = Relationship.OUTGOING) // type设定了一个关系的类型,direction设定了这个关系的方向。Relationship.INCOMING表明该节点是终点。
    List<Role> roles = new ArrayList<>(); // @Relationship表示List<Role>是一个关系列表。

   // addRole定义了增加一个关系的方法
    public Role addRole(Actor actor, String name){
        Role role = new Role(actor,this, name);
        this.roles.add(role);
        return role;
    }

    public Movie() { }

    public Long getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public List<Role> getRoles() {
        return roles;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

Role

@RelationshipEntity(type = "角色") // @Relationship表明这个类是一个关系实体,并用type指定了关系的类型
public class Role {
    @Id
    @GeneratedValue
    Long id;
    String name;

    @StartNode
    Actor actor; // StartNode指定起始节点的实体
    @EndNode
    Movie movie; // EndNode指定终止节点的实体
    

    public Role() {
    }

    public Role(Actor actor, Movie movie, String name) {
        this.actor = actor;
        this.movie = movie;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Actor getActor() {
        return actor;
    }

    public Movie getMovie() {
        return movie;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

2.4.3 节点实体持久化

像其他数据库的访问和存取等操作一样,spring-data-neo4j提供了功能丰富的资源库可供调用。因此,对于Actor和Movie Entity,可以创建他们对应的资源库接口,实现实体的持久化。关系实体却不用实现持久化,当保存节点实体时,节点实体的关系将会同时保存。

movie repository

@Repository
public interface MovieRepository extends GraphRepository<Movie> {
    Movie findByTitle(@Param("title") String title);
}

  • 1
  • 2
  • 3
  • 4
  • 5

actor repository

@Repository
public interface ActorRepository extends GraphRepository<Actor> {
}

  • 1
  • 2
  • 3
  • 4

2.4.4 Neo4j测试

都是类似的,就不讲解了。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/727691
推荐阅读
相关标签
  

闽ICP备14008679号