Total Pageviews

2018/10/07

[Neo4j] Using Spring data to find nodes and its relationships

假設我要從下圖中,分別找出
(1) 大雄的朋友
(2) 大雄的父母
(3) 大雄的寵物,以及寵物的妹妹

架構規劃如下:

pom.xml 內容
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>neo4j</groupId>
 <artifactId>springdata</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>

 <name>springdata</name>
 <url>http://maven.apache.org</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <java.version>1.8</java.version>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
 </properties>

 <dependencies>
  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-neo4j</artifactId>
      <version>2.0.0.RELEASE</version>
  </dependency>

  <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.16.18</version>
  </dependency>

  <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>23.0</version>
  </dependency>

  <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
  </dependency>
 </dependencies>

 <repositories>
  <repository>
      <id>spring-libs-release</id>
      <name>Spring Releases</name>
      <url>https://repo.spring.io/libs-release</url>
      <snapshots>
   <enabled>false</enabled>
       </snapshots>
  </repository>
 </repositories>
</project>


Value Object - Person
package neo4j.springdata.vo;

import java.util.ArrayList;
import java.util.List;

import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;

import lombok.Data;
import lombok.ToString;

@NodeEntity
@Data
@ToString
public class Person {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    private String gender;
    
    @Relationship(type = "FRIEND_OF")
    public List<Person> friends = new ArrayList<Person>();
    
}

Value Object - Robot
package neo4j.springdata.vo;

import org.neo4j.ogm.annotation.GeneratedValue;
import org.neo4j.ogm.annotation.Id;
import org.neo4j.ogm.annotation.NodeEntity;

import lombok.Data;

@NodeEntity
@Data
public class Robot {
    @Id
    @GeneratedValue
    private Long id;
    
    private String name;
    
    private String gender;
    
}

Value Object - PetQueryResult (查詢結果)
package neo4j.springdata.vo.result;

import org.springframework.data.neo4j.annotation.QueryResult;

import lombok.Data;
import lombok.ToString;
import neo4j.springdata.vo.Person;
import neo4j.springdata.vo.Robot;

@QueryResult
@Data
@ToString
public class PetQueryResult {

    private Person person;
    private Robot pet;
    private Robot petSister;

}

Repository class - PersonRepository
package neo4j.springdata.repository;

import java.util.List;

import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import neo4j.springdata.vo.Person;
import neo4j.springdata.vo.result.PetQueryResult;

@Repository
public interface PersonRepository extends CrudRepository<Person, Long> {

    @Query("match(n) return n")
    List<Person> findAll();

    Person findByName(String name);

    @Query(" match(p:Person) where p.name={name} " +
           " optional match (p)-[:FRIEND_OF]->(p2) " + 
           " return p2")
    List<Person> findFriendsByName(@Param("name") String name);

    @Query(" match(p:Person) where p.name={name} " +
           " optional match (p)-[:FATHER_OF]-(p2) " +
           " return p2 " +
           " union " +
           " match(p:Person) where p.name={name} " +
           " optional match (p)-[:MOTHER_OF]-(p2) " +
           " return p2 ")
    List<Person> findParents(@Param("name") String name);
    
    @Query(" match(p:Person)-[:PET_OF]-(r:Robot)-[:SISTER_OF]-(r2) " +
           " where p.name={name} " +
           " return p as person, r as pet, r2 as petSister ")
    List<PetQueryResult> findPetsAndItsSister(@Param("name") String name);
    
}

Service class - PersonService
package neo4j.springdata.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import neo4j.springdata.repository.PersonRepository;
import neo4j.springdata.vo.Person;
import neo4j.springdata.vo.result.PetQueryResult;

@Service
public class PersonService {

    @Autowired
    private PersonRepository personRepo;

    public List<Person> findAll() {
        return personRepo.findAll();
    }

    public List<Person> findFriendsByName(String name) {
        return personRepo.findFriendsByName(name);
    }

    public List<Person> findParents(String name) {
        return personRepo.findParents(name);
    }

    public List<PetQueryResult> findPetsAndItsSister(String name) {
        return personRepo.findPetsAndItsSister(name);
    }

}

Client class - App
package neo4j.springdata;

import java.util.List;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories;

import lombok.extern.slf4j.Slf4j;
import neo4j.springdata.service.PersonService;
import neo4j.springdata.vo.Person;
import neo4j.springdata.vo.Robot;
import neo4j.springdata.vo.result.PetQueryResult;

@SpringBootApplication
@EnableNeo4jRepositories
@Slf4j
public class App {

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Bean
    CommandLineRunner findFriendsByName(PersonService service) {
        return args -> {
            log.info("find 大雄的朋友:");
            List<Person> friends = service.findFriendsByName("大雄");
            friends.forEach(p -> log.info(p.toString()));

            log.info("find 大雄的父母:");
            List<Person> parents = service.findParents("大雄");
            parents.forEach(p -> log.info(p.toString()));

            List<PetQueryResult> petQueryResult = service.findPetsAndItsSister("大雄");
            petQueryResult.forEach(p -> {
                Robot pet = p.getPet();
                Robot petSister = p.getPetSister();
                log.info("大雄的寵物是[" + pet.getName() + "], 寵物的妹妹是[" + petSister.getName() + "]");
            });
        };
    }

}


No comments: