@Override
  public void run(String... strings) throws Exception {

    Person greg = new Person("Greg");
    Person roy = new Person("Roy");
    Person craig = new Person("Craig");

    System.out.println("Before linking up with Neo4j...");
    for (Person person : new Person[] {greg, roy, craig}) {
      System.out.println(person);
    }

    Transaction tx = graphDatabase.beginTx();
    try {
      personRepository.save(greg);
      personRepository.save(roy);
      personRepository.save(craig);

      greg = personRepository.findByName(greg.name);
      greg.worksWith(roy);
      greg.worksWith(craig);
      personRepository.save(greg);

      roy = personRepository.findByName(roy.name);
      roy.worksWith(craig);
      // We already know that roy works with greg
      personRepository.save(roy);

      // We already know craig works with roy and greg

      System.out.println("Lookup each person by name...");
      for (String name : new String[] {greg.name, roy.name, craig.name}) {
        System.out.println(personRepository.findByName(name));
      }

      System.out.println("Looking up who works with Greg...");
      for (Person person : personRepository.findByTeammatesName("Greg")) {
        System.out.println(person.name + " works with Greg.");
      }

      tx.success();
    } finally {

      // tx.close();
    }
  }
Пример #2
0
  @Override
  public void run(String... strings) throws Exception {
    Person greg = new Person("Greg");
    Person craig = new Person("Craig");
    Person roy = new Person("Roy");

    List<Person> people = Arrays.asList(greg, craig, roy);

    System.out.println("Before linking up with Neo4j...");
    people.stream().forEach(System.out::println);

    try (Transaction tx = graphDatabase.beginTx()) {
      personRepository.save(greg);
      personRepository.save(craig);
      personRepository.save(roy);

      greg = personRepository.findByName(greg.name);
      greg.worksWith(roy);
      greg.worksWith(craig);
      personRepository.save(greg);

      roy = personRepository.findByName(roy.name);
      roy.worksWith(craig);
      personRepository.save(roy);

      System.out.println("Lookup each person by name...");
      people
          .stream()
          .map(Person::getName)
          .map(personRepository::findByName)
          .forEach(System.out::println);

      System.out.println("Lookup who works with Greg...");
      StreamSupport.stream(personRepository.findByTeammatesName("Greg").spliterator(), false)
          .map(Person::getName)
          .forEach(name -> System.out.println(name + " works with Greg"));
      tx.success();
    }
  }