@Test
 public void shouldRollbackViaStatus() throws Exception {
   new TransactionTemplate(neo4jTransactionManager)
       .execute(
           new TransactionCallbackWithoutResult() {
             @Override
             protected void doInTransactionWithoutResult(final TransactionStatus status) {
               neo4jTemplate.exec(
                   new GraphCallback.WithoutResult() {
                     @Override
                     public void doWithGraphWithoutResult(GraphDatabase graph) throws Exception {
                       graph
                           .getReferenceNode()
                           .setProperty("test", "shouldRollbackTransactionOnException");
                       status.setRollbackOnly();
                     }
                   });
             }
           });
   Transaction tx = graphDatabase.beginTx();
   try {
     Assert.assertThat(
         (String) graphDatabase.getReferenceNode().getProperty("test", "not set"),
         not("shouldRollbackTransactionOnException"));
   } finally {
     tx.success();
     tx.finish();
   }
 }
 @Test
 @Transactional
 public void testIndexRelationship() throws Exception {
   Index<Relationship> index = graphDatabase.getIndex("relationship");
   Relationship lookedUpRelationship = index.get("name", "rel1").getSingle();
   assertThat("same relationship from index", lookedUpRelationship, is(relationship1));
 }
 @Test
 @Transactional
 public void testIndexNode() throws Exception {
   neo4jTemplate.index("node", node1, "name", "node1");
   Index<Node> index = graphDatabase.getIndex("node");
   Node lookedUpNode = index.get("name", "node1").getSingle();
   assertThat("same node from index", lookedUpNode, is(node1));
 }
Esempio n. 4
0
  // Ensure neo4j is embedded and browser is started at localhost:7575
  @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<?> hello(
      @RequestHeader HttpHeaders headers,
      @RequestParam(value = "row", required = false) Integer row,
      @RequestParam(value = "size", required = false) Integer size)
      throws Exception {

    Human greg = new Human("Greg");
    Human roy = new Human("Roy");
    Human craig = new Human("Craig");
    Human ivan = new Human("Ivan");

    LOG.info("Before linking up with Neo4j...");
    for (Human human : new Human[] {greg, roy, craig, ivan}) {
      LOG.info(human.toString());
    }

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

      greg = humanRepository.findByName(greg.name);
      greg.worksWith(roy);
      greg.worksWith(craig);
      greg.friendsWith(ivan);
      humanRepository.save(greg);

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

      // We already know craig works with roy and greg

      LOG.info("Lookup each human by name...");
      for (String name : new String[] {greg.name, roy.name, craig.name}) {
        LOG.info(humanRepository.findByName(name).toString());
      }

      LOG.info("Looking up who works with Greg...");
      for (Human human : humanRepository.findByTeammatesName("Greg")) {
        LOG.info(human.name + " works with Greg.");
      }

      tx.success();
    } finally {
      tx.close();
    }

    return new ResponseEntity<>(new String(), HttpStatus.OK);
  }
  @Test
  public void shouldRollbackTransactionOnException() {
    try {
      neo4jTemplate.exec(
          new GraphCallback.WithoutResult() {
            @Override
            public void doWithGraphWithoutResult(GraphDatabase graph) throws Exception {
              graph.getReferenceNode().setProperty("test", "shouldRollbackTransactionOnException");
              throw new RuntimeException("please rollback");
            }
          });
    } catch (RuntimeException re) {

    }
    Transaction tx = graphDatabase.beginTx();
    try {
      Assert.assertThat(
          (String) graphDatabase.getReferenceNode().getProperty("test", "not set"),
          not("shouldRollbackTransactionOnException"));
    } finally {
      tx.success();
      tx.finish();
    }
  }
  @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();
    }
  }
 @Before
 public void setUp() throws Exception {
   Transaction tx = neo4jTemplate.getGraphDatabase().beginTx();
   try {
     Neo4jHelper.cleanDb(neo4jTemplate);
   } finally {
     tx.success();
     tx.finish();
   }
   tx = neo4jTemplate.getGraphDatabase().beginTx();
   try {
     referenceNode = graphDatabase.getReferenceNode();
     createData();
   } finally {
     tx.success();
     tx.finish();
   }
 }
 @Test
 public void shouldExecuteCallbackInTransaction() throws Exception {
   Node refNode =
       neo4jTemplate.exec(
           new GraphCallback<Node>() {
             @Override
             public Node doWithGraph(GraphDatabase graph) throws Exception {
               Node referenceNode = graph.getReferenceNode();
               referenceNode.setProperty("test", "testDoInTransaction");
               return referenceNode;
             }
           });
   Transaction tx = graphDatabase.beginTx();
   try {
     assertEquals("same reference node", referenceNode, refNode);
     assertTestPropertySet(referenceNode, "testDoInTransaction");
   } finally {
     tx.success();
     tx.finish();
   }
 }
Esempio n. 9
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();
    }
  }
 public SchemaIndexProvider(GraphDatabase gd) {
   this.gd = gd;
   cypher = gd.queryEngine();
 }
  @Override
  public void afterPropertiesSet() {
    try {
      if (this.mappingContext == null) this.mappingContext = new Neo4jMappingContext();
      if (this.graphDatabaseService == null && graphDatabase instanceof DelegatingGraphDatabase) {
        this.graphDatabaseService =
            ((DelegatingGraphDatabase) graphDatabase).getGraphDatabaseService();
      }
      if (this.graphDatabase == null) {
        this.graphDatabase = new DelegatingGraphDatabase(graphDatabaseService);
      }
      if (this.transactionManager == null) {
        this.transactionManager = new JtaTransactionManager(graphDatabase.getTransactionManager());
      }
      if (this.conversionService == null) {
        this.conversionService = new Neo4jConversionServiceFactoryBean().getObject();
      }
      if (nodeEntityInstantiator == null) {
        nodeEntityInstantiator = new NodeEntityInstantiator(entityStateHandler);
      }
      if (relationshipEntityInstantiator == null) {
        relationshipEntityInstantiator = new RelationshipEntityInstantiator(entityStateHandler);
      }
      if (this.typeRepresentationStrategyFactory == null) {
        this.typeRepresentationStrategyFactory =
            new TypeRepresentationStrategyFactory(graphDatabase);
      }
      if (this.nodeTypeRepresentationStrategy == null) {
        this.nodeTypeRepresentationStrategy =
            typeRepresentationStrategyFactory.getNodeTypeRepresentationStrategy();
      }
      if (this.relationshipTypeRepresentationStrategy == null) {
        this.relationshipTypeRepresentationStrategy =
            typeRepresentationStrategyFactory.getRelationshipTypeRepresentationStrategy();
      }
      if (this.nodeEntityStateFactory == null) {
        this.nodeEntityStateFactory =
            new NodeEntityStateFactory(
                mappingContext, new NodeDelegatingFieldAccessorFactory.Factory());
      }
      if (this.relationshipEntityStateFactory == null) {
        this.relationshipEntityStateFactory =
            new RelationshipEntityStateFactory(
                mappingContext, new RelationshipDelegatingFieldAccessorFactory.Factory());
      }
      this.typeRepresentationStrategies =
          new TypeRepresentationStrategies(
              mappingContext,
              nodeTypeRepresentationStrategy,
              relationshipTypeRepresentationStrategy);

      final EntityStateHandler entityStateHandler =
          new EntityStateHandler(mappingContext, graphDatabase);
      EntityTools<Node> nodeEntityTools =
          new EntityTools<Node>(
              nodeTypeRepresentationStrategy,
              nodeEntityStateFactory,
              nodeEntityInstantiator,
              mappingContext);
      EntityTools<Relationship> relationshipEntityTools =
          new EntityTools<Relationship>(
              relationshipTypeRepresentationStrategy,
              relationshipEntityStateFactory,
              relationshipEntityInstantiator,
              mappingContext);
      this.entityPersister =
          new Neo4jEntityPersister(
              conversionService,
              nodeEntityTools,
              relationshipEntityTools,
              mappingContext,
              entityStateHandler);
      this.entityRemover =
          new EntityRemover(
              this.entityStateHandler,
              nodeTypeRepresentationStrategy,
              relationshipTypeRepresentationStrategy,
              graphDatabase);
      if (this.resultConverter == null) {
        this.resultConverter = new EntityResultConverter<Object, Object>(conversionService);
      }
      this.graphDatabase.setResultConverter(resultConverter);
      this.cypherQueryExecutor =
          new CypherQueryExecutor(graphDatabase.queryEngineFor(QueryType.Cypher, resultConverter));
      if (this.indexProvider == null) {
        this.indexProvider = new IndexProviderImpl(this.mappingContext, graphDatabase);
      }
      this.mappingInfrastructure =
          new MappingInfrastructure(
              graphDatabase,
              graphDatabaseService,
              indexProvider,
              resultConverter,
              transactionManager,
              typeRepresentationStrategies,
              entityRemover,
              entityPersister,
              entityStateHandler,
              cypherQueryExecutor,
              mappingContext,
              relationshipTypeRepresentationStrategy,
              nodeTypeRepresentationStrategy,
              validator,
              conversionService);
    } catch (Exception e) {
      throw new RuntimeException("error initializing " + getClass().getName(), e);
    }
  }