org.neo4j.driver.types.Node Java Examples
The following examples show how to use
org.neo4j.driver.types.Node.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void loadEntityWithRelationshipWithAssignedId(@Autowired ReactivePetRepository repository) { long petNodeId; try (Session session = createSession()) { Record record = session .run("CREATE (p:Pet{name:'Jerry'})-[:Has]->(t:Thing{theId:'t1', name:'Thing1'}) " + "RETURN p, t").single(); Node petNode = record.get("p").asNode(); petNodeId = petNode.id(); } StepVerifier.create(repository.findById(petNodeId)) .assertNext(pet -> { ThingWithAssignedId relatedThing = pet.getThings().get(0); assertThat(relatedThing.getTheId()).isEqualTo("t1"); assertThat(relatedThing.getName()).isEqualTo("Thing1"); }) .verifyComplete(); }
Example #2
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void loadEntityWithBidirectionalRelationshipFromIncomingSide(@Autowired BidirectionalEndRepository repository) { long endId; try (Session session = createSession()) { Record record = session .run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}) " + "RETURN e").single(); Node endNode = record.get("e").asNode(); endId = endNode.id(); } StepVerifier.create(repository.findById(endId)) .assertNext(entity -> { assertThat(entity.getStart()).isNotNull(); }) .verifyComplete(); }
Example #3
Source File: AuditingITBase.java From sdn-rx with Apache License 2.0 | 6 votes |
private void assertDataMatch(AuditableThing expectedValues, Node node) { assertThat(node.get("name").asString()).isEqualTo(expectedValues.getName()); assertThat(node.get("createdAt").asLocalDateTime()).isEqualTo(expectedValues.getCreatedAt()); assertThat(node.get("createdBy").asString()).isEqualTo(expectedValues.getCreatedBy()); Value modifiedAt = node.get("modifiedAt"); Value modifiedBy = node.get("modifiedBy"); if (expectedValues.getModifiedAt() == null) { assertThat(modifiedAt.isNull()).isTrue(); } else { assertThat(modifiedAt.asLocalDateTime()).isEqualTo(expectedValues.getModifiedAt()); } if (expectedValues.getModifiedBy() == null) { assertThat(modifiedBy.isNull()).isTrue(); } else { assertThat(modifiedBy.asString()).isEqualTo(expectedValues.getModifiedBy()); } }
Example #4
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void findEntityWithBidirectionalRelationship(@Autowired BidirectionalStartRepository repository) { long startId; try (Session session = createSession()) { Record record = session .run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}), " + "(e)<-[:ANOTHER_CONNECTION]-(anotherStart:BidirectionalStart{name:'Elmo'})" + "RETURN n").single(); Node startNode = record.get("n").asNode(); startId = startNode.id(); } Optional<BidirectionalStart> entityOptional = repository.findById(startId); assertThat(entityOptional).isPresent(); BidirectionalStart entity = entityOptional.get(); assertThat(entity.getEnds()).hasSize(1); BidirectionalEnd end = entity.getEnds().iterator().next(); assertThat(end.getAnotherStart()).isNotNull(); assertThat(end.getAnotherStart().getName()).isEqualTo("Elmo"); }
Example #5
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void findEntityWithBidirectionalRelationshipFromIncomingSide(@Autowired BidirectionalEndRepository repository) { long endId; try (Session session = createSession()) { Record record = session .run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}) " + "RETURN e").single(); Node endNode = record.get("e").asNode(); endId = endNode.id(); } Optional<BidirectionalEnd> entityOptional = repository.findById(endId); assertThat(entityOptional).isPresent(); BidirectionalEnd entity = entityOptional.get(); assertThat(entity.getStart()).isNotNull(); }
Example #6
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void loadEntityWithBidirectionalRelationship(@Autowired BidirectionalStartRepository repository) { long startId; try (Session session = createSession()) { Record record = session .run("CREATE (n:BidirectionalStart{name:'Ernie'})-[:CONNECTED]->(e:BidirectionalEnd{name:'Bert'}) " + "RETURN n").single(); Node startNode = record.get("n").asNode(); startId = startNode.id(); } StepVerifier.create(repository.findById(startId)) .assertNext(entity -> { assertThat(entity.getEnds()).hasSize(1); }) .verifyComplete(); }
Example #7
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void findEntityWithRelationshipWithAssignedId(@Autowired PetRepository repository) { long petNodeId; try (Session session = createSession()) { Record record = session .run("CREATE (p:Pet{name:'Jerry'})-[:Has]->(t:Thing{theId:'t1', name:'Thing1'}) " + "RETURN p, t").single(); Node petNode = record.get("p").asNode(); petNodeId = petNode.id(); } Pet pet = repository.findById(petNodeId).get(); ThingWithAssignedId relatedThing = pet.getThings().get(0); assertThat(relatedThing.getTheId()).isEqualTo("t1"); assertThat(relatedThing.getName()).isEqualTo("Thing1"); }
Example #8
Source File: Neo4JSession.java From neo4j-gremlin-bolt with Apache License 2.0 | 6 votes |
private Vertex loadVertex(Record record) { // node Node node = record.get(0).asNode(); // vertex id Object vertexId = vertexIdProvider.get(node); // check vertex has been deleted if (!deletedVertices.contains(vertexId)) { // check this vertex has been already loaded into this session Vertex vertex = vertices.get(vertexId); if (vertex == null) { // check node belongs to partition if (partition.containsVertex(StreamSupport.stream(node.labels().spliterator(), false).collect(Collectors.toSet()))) { // create and register vertex return registerVertex(new Neo4JVertex(graph, this, vertexIdProvider, edgeIdProvider, node)); } // skip vertex (not in partition) return null; } // return vertex return vertex; } // skip vertex (deleted) return null; }
Example #9
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void createNodeAndRelationshipWithMultipleLabels( @Autowired MultipleLabelWithAssignedIdRepository multipleLabelRepository) { MultipleLabels.MultipleLabelsEntityWithAssignedId entity = new MultipleLabels.MultipleLabelsEntityWithAssignedId( 4711L); entity.otherMultipleLabelEntity = new MultipleLabels.MultipleLabelsEntityWithAssignedId(42L); multipleLabelRepository.save(entity); try (Session session = createSession()) { Record record = session.run("MATCH (n:X)-[:HAS]->(c:X) return n, c").single(); Node parentNode = record.get("n").asNode(); Node childNode = record.get("c").asNode(); assertThat(parentNode.labels()).containsExactlyInAnyOrder("X", "Y", "Z"); assertThat(childNode.labels()).containsExactlyInAnyOrder("X", "Y", "Z"); } }
Example #10
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void saveSingleEntity(@Autowired PersonRepository repository) { PersonWithAllConstructor person = new PersonWithAllConstructor(null, "Mercury", "Freddie", "Queen", true, 1509L, LocalDate.of(1946, 9, 15), null, Arrays.asList("b", "a"), null, null); PersonWithAllConstructor savedPerson = repository.save(person); try (Session session = createSession()) { Record record = session .run("MATCH (n:PersonWithAllConstructor) WHERE n.first_name = $first_name RETURN n", Values.parameters("first_name", "Freddie")).single(); assertThat(record.containsKey("n")).isTrue(); Node node = record.get("n").asNode(); assertThat(savedPerson.getId()).isEqualTo(node.id()); assertThat(node.get("things").asList()).containsExactly("b", "a"); } }
Example #11
Source File: CallbacksITBase.java From sdn-rx with Apache License 2.0 | 6 votes |
protected void verifyDatabase(Iterable<ThingWithAssignedId> expectedValues) { List<String> ids = StreamSupport.stream(expectedValues.spliterator(), false) .map(ThingWithAssignedId::getTheId).collect(toList()); List<String> names = StreamSupport.stream(expectedValues.spliterator(), false) .map(ThingWithAssignedId::getName).collect(toList()); try (Session session = driver.session()) { Record record = session .run("MATCH (n:Thing) WHERE n.theId in $ids RETURN COLLECT(n) as things", Values.parameters("ids", ids)) .single(); List<Node> nodes = record.get("things").asList(Value::asNode); assertThat(nodes).extracting(n -> n.get("theId").asString()).containsAll(ids); assertThat(nodes).extracting(n -> n.get("name").asString()) .containsAll(names); } }
Example #12
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void saveWithAssignedId(@Autowired ThingRepository repository) { assertThat(repository.count()).isEqualTo(21); ThingWithAssignedId thing = new ThingWithAssignedId("aaBB"); thing.setName("That's the thing."); thing = repository.save(thing); try (Session session = createSession()) { Record record = session .run("MATCH (n:Thing) WHERE n.theId = $id RETURN n", Values.parameters("id", thing.getTheId())) .single(); assertThat(record.containsKey("n")).isTrue(); Node node = record.get("n").asNode(); assertThat(node.get("theId").asString()).isEqualTo(thing.getTheId()); assertThat(node.get("name").asString()).isEqualTo(thing.getName()); assertThat(repository.count()).isEqualTo(22); } }
Example #13
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 6 votes |
@Test void createNodeAndRelationshipWithMultipleLabels(@Autowired ReactiveMultipleLabelWithAssignedIdRepository repository) { MultipleLabels.MultipleLabelsEntityWithAssignedId entity = new MultipleLabels.MultipleLabelsEntityWithAssignedId(4711L); entity.otherMultipleLabelEntity = new MultipleLabels.MultipleLabelsEntityWithAssignedId(42L); repository.save(entity).block(); try (Session session = createSession()) { Record record = session.run("MATCH (n:X)-[:HAS]->(c:X) return n, c").single(); Node parentNode = record.get("n").asNode(); Node childNode = record.get("c").asNode(); assertThat(parentNode.labels()).containsExactlyInAnyOrder("X", "Y", "Z"); assertThat(childNode.labels()).containsExactlyInAnyOrder("X", "Y", "Z"); } }
Example #14
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void createNodeWithMultipleLabels(@Autowired ReactiveMultipleLabelRepository repository) { repository.save(new MultipleLabels.MultipleLabelsEntity()).block(); try (Session session = createSession()) { Node node = session.run("MATCH (n:A) return n").single().get("n").asNode(); assertThat(node.labels()).containsExactlyInAnyOrder("A", "B", "C"); } }
Example #15
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void createAllNodesWithMultipleLabels(@Autowired ReactiveMultipleLabelWithAssignedIdRepository repository) { repository.saveAll(singletonList(new MultipleLabels.MultipleLabelsEntityWithAssignedId(4711L))) .collectList().block(); try (Session session = createSession()) { Node node = session.run("MATCH (n:X) return n").single().get("n").asNode(); assertThat(node.labels()).containsExactlyInAnyOrder("X", "Y", "Z"); } }
Example #16
Source File: IdGeneratorsITBase.java From sdn-rx with Apache License 2.0 | 5 votes |
protected void verifyDatabase(String id, String name) { try (Session session = driver.session()) { Node node = session .run("MATCH (t) WHERE t.theId = $theId RETURN t", Values.parameters("theId", id)) .single().get("t").asNode(); assertThat(node.get("name").asString()).isEqualTo(name); assertThat(node.get("theId").asString()).isEqualTo(id); } }
Example #17
Source File: DefaultNeo4jConverter.java From sdn-rx with Apache License 2.0 | 5 votes |
private static Value extractValueOf(Neo4jPersistentProperty property, MapAccessor propertyContainer) { if (property.isInternalIdProperty()) { return propertyContainer instanceof Node ? Values.value(((Node) propertyContainer).id()) : propertyContainer.get(NAME_OF_INTERNAL_ID); } else { String graphPropertyName = property.getPropertyName(); return propertyContainer.get(graphPropertyName); } }
Example #18
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void createNodeWithMultipleLabels(@Autowired MultipleLabelRepository multipleLabelRepository) { multipleLabelRepository.save(new MultipleLabels.MultipleLabelsEntity()); try (Session session = createSession()) { Node node = session.run("MATCH (n:A) return n").single().get("n").asNode(); assertThat(node.labels()).containsExactlyInAnyOrder("A", "B", "C"); } }
Example #19
Source File: RepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void saveAllWithAssignedIdAndRelationship(@Autowired ThingRepository repository) { ThingWithAssignedId thing = new ThingWithAssignedId("aaBB"); thing.setName("That's the thing."); AnotherThingWithAssignedId anotherThing = new AnotherThingWithAssignedId(4711L); anotherThing.setName("AnotherThing"); thing.setThings(singletonList(anotherThing)); repository.saveAll(singletonList(thing)); try (Session session = createSession()) { Record record = session .run("MATCH (n:Thing)-[:Has]->(t:Thing2) WHERE n.theId = $id RETURN n, t", Values.parameters("id", thing.getTheId())) .single(); assertThat(record.containsKey("n")).isTrue(); assertThat(record.containsKey("t")).isTrue(); Node node = record.get("n").asNode(); assertThat(node.get("theId").asString()).isEqualTo(thing.getTheId()); assertThat(node.get("name").asString()).isEqualTo(thing.getName()); Node relatedNode = record.get("t").asNode(); assertThat(relatedNode.get("theId").asLong()).isEqualTo(anotherThing.getTheId()); assertThat(relatedNode.get("name").asString()).isEqualTo(anotherThing.getName()); assertThat(repository.count()).isEqualTo(1); } }
Example #20
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void createAllNodesWithMultipleLabels(@Autowired ReactiveMultipleLabelRepository repository) { repository.saveAll(singletonList(new MultipleLabels.MultipleLabelsEntity())).collectList().block(); try (Session session = createSession()) { Node node = session.run("MATCH (n:A) return n").single().get("n").asNode(); assertThat(node.labels()).containsExactlyInAnyOrder("A", "B", "C"); } }
Example #21
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void createNodeAndRelationshipWithMultipleLabels(@Autowired ReactiveMultipleLabelRepository labelRepository) { MultipleLabels.MultipleLabelsEntity entity = new MultipleLabels.MultipleLabelsEntity(); entity.otherMultipleLabelEntity = new MultipleLabels.MultipleLabelsEntity(); labelRepository.save(entity).block(); try (Session session = createSession()) { Record record = session.run("MATCH (n:A)-[:HAS]->(c:A) return n, c").single(); Node parentNode = record.get("n").asNode(); Node childNode = record.get("c").asNode(); assertThat(parentNode.labels()).containsExactlyInAnyOrder("A", "B", "C"); assertThat(childNode.labels()).containsExactlyInAnyOrder("A", "B", "C"); } }
Example #22
Source File: ReactiveRepositoryIT.java From sdn-rx with Apache License 2.0 | 5 votes |
@Test void createNodeWithMultipleLabelsAndAssignedId(@Autowired ReactiveMultipleLabelWithAssignedIdRepository repository) { repository.save(new MultipleLabels.MultipleLabelsEntityWithAssignedId(4711L)).block(); try (Session session = createSession()) { Node node = session.run("MATCH (n:X) return n").single().get("n").asNode(); assertThat(node.labels()).containsExactlyInAnyOrder("X", "Y", "Z"); } }
Example #23
Source File: DefaultNeo4jConverter.java From sdn-rx with Apache License 2.0 | 5 votes |
/** * Returns the list of labels for the entity to be created from the "main" node returned. * * @param queryResult The complete query result * @return The list of labels defined by the query variable {@link org.neo4j.springframework.data.core.schema.Constants#NAME_OF_LABELS}. */ @NonNull private List<String> getLabels(MapAccessor queryResult) { Value labelsValue = queryResult.get(NAME_OF_LABELS); List<String> labels = new ArrayList<>(); if (!labelsValue.isNull()) { labels = labelsValue.asList(Value::asString); } else if (queryResult instanceof Node) { Node nodeRepresentation = (Node) queryResult; nodeRepresentation.labels().forEach(labels::add); } return labels; }
Example #24
Source File: MovieRepository.java From sdn-rx with Apache License 2.0 | 5 votes |
public Optional<Movie> findByTitle(String title) { try (Session session = driver.session()) { Record r = session.run("MATCH (m:Movie {title: $title}) RETURN m", Values.parameters("title", title)) .single(); Node movieNode = r.get("m").asNode(); return Optional .of(new Movie(movieNode.id(), movieNode.get("title").asString(), movieNode.get("tagline").asString())); } catch (NoSuchRecordException e) { return Optional.empty(); } }
Example #25
Source File: MovieRepository.java From sdn-rx with Apache License 2.0 | 5 votes |
public Movie save(Movie movie) { try (Session session = driver.session()) { Record r = session.run("CREATE (m:Movie {title: $title, tagline: $tagline}) RETURN m", Values.parameters("title", movie.getTitle(), "tagline", movie.getTagline())) .single(); Node movieNode = r.get("m").asNode(); return new Movie(movieNode.id(), movieNode.get("title").asString(), movieNode.get("tagline").asString()); } }
Example #26
Source File: RemoteEntityConverter.java From extended-objects with Apache License 2.0 | 5 votes |
@Override public Object convert(Object value) { if (value instanceof Node) { return sessionCache.getNode((Node) value); } else if (value instanceof Relationship) { return sessionCache.getRelationship((Relationship) value); } throw new XOException("Unsupported value type " + value); }
Example #27
Source File: RemoteDatastoreRelationManager.java From extended-objects with Apache License 2.0 | 5 votes |
@Override public RemoteRelationship findRelationById(RelationTypeMetadata<RelationshipMetadata<RemoteRelationshipType>> metadata, Long id) { String statement = String.format("MATCH (start)-[r:%s]->(end) WHERE id(r)=$id RETURN start,r,end", metadata.getDatastoreMetadata().getDiscriminator().getName()); Record record = statementExecutor.getSingleResult(statement, parameters("id", id)); Node start = record.get("start").asNode(); Relationship relationship = record.get("r").asRelationship(); Node end = record.get("end").asNode(); return datastoreSessionCache.getRelationship(start, relationship, end); }
Example #28
Source File: RemoteDatastoreRelationManager.java From extended-objects with Apache License 2.0 | 5 votes |
private StateTracker<RemoteRelationship, Set<RemoteRelationship>> getRelationships(RemoteNode source, RemoteRelationshipType type, RemoteDirection remoteDirection) { StateTracker<RemoteRelationship, Set<RemoteRelationship>> trackedRelationships = source.getState().getRelationships(remoteDirection, type); if (trackedRelationships == null) { String sourceIdentifier; switch (remoteDirection) { case OUTGOING: sourceIdentifier = "start"; break; case INCOMING: sourceIdentifier = "end"; break; default: throw new XOException("Direction not supported: " + remoteDirection); } String statement = String.format("MATCH (start)-[r:%s]->(end) WHERE id(%s)=$id RETURN start,r,end", type.getName(), sourceIdentifier); Result statementResult = statementExecutor.execute(statement, parameters("id", source.getId())); Set<RemoteRelationship> loaded = new LinkedHashSet<>(); try { while (statementResult.hasNext()) { Record record = statementResult.next(); Node start = record.get("start").asNode(); Relationship relationship = record.get("r").asRelationship(); Node end = record.get("end").asNode(); RemoteRelationship remoteRelationship = datastoreSessionCache.getRelationship(start, relationship, end); loaded.add(remoteRelationship); } } finally { statementResult.consume(); } trackedRelationships = new StateTracker<>(loaded); source.getState().setRelationships(remoteDirection, type, trackedRelationships); } return trackedRelationships; }
Example #29
Source File: RemoteDatastoreSessionCache.java From extended-objects with Apache License 2.0 | 5 votes |
public RemoteNode getNode(Node node) { RemoteNode remoteNode = getNode(node.id()); NodeState nodeState = remoteNode.getState(); if (!nodeState.isLoaded()) { nodeState.load(node.asMap()); Set<RemoteLabel> labels = new HashSet<>(); for (String label : node.labels()) { labels.add(new RemoteLabel(label)); } nodeState.getLabels().load(labels); } return remoteNode; }
Example #30
Source File: RemoteDatastoreEntityManager.java From extended-objects with Apache License 2.0 | 5 votes |
@Override public ResultIterator<RemoteNode> findEntity(EntityTypeMetadata<NodeMetadata<RemoteLabel>> type, RemoteLabel remoteLabel, Map<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> values) { if (values.size() > 1) { throw new XOException("Only one property value is supported for find operation"); } Map.Entry<PrimitivePropertyMethodMetadata<PropertyMetadata>, Object> entry = values.entrySet().iterator().next(); PropertyMetadata propertyMetadata = getIndexedPropertyMetadata(type, entry.getKey()); Object value = entry.getValue(); String statement = String.format("MATCH (n:%s) WHERE n.%s=$v RETURN n", remoteLabel.getName(), propertyMetadata.getName()); Result result = statementExecutor.execute(statement, parameters("v", value)); return new ResultIterator<RemoteNode>() { @Override public boolean hasNext() { return result.hasNext(); } @Override public RemoteNode next() { Record record = result.next(); Node node = record.get("n").asNode(); return datastoreSessionCache.getNode(node); } @Override public void close() { result.consume(); } }; }