Search in sources :

Example 1 with Neo4jPersistentProperty

use of org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty in project spring-data-neo4j by spring-projects.

the class Neo4jTemplateIT method saveAllAsWithDynamicProjectionOnSecondLevelShouldWork.

// GH-2420
@Test
void saveAllAsWithDynamicProjectionOnSecondLevelShouldWork() {
    Person p = neo4jTemplate.findOne("MATCH (p:Person {lastName: $lastName})-[r:LIVES_AT]-(a:Address) RETURN p, collect(r), collect(a)", Collections.singletonMap("lastName", "Siemons"), Person.class).get();
    p.setFirstName("Klaus");
    p.setLastName("Simons");
    p.getAddress().setCity("Braunschweig");
    p.getAddress().setStreet("Single Trail");
    Person.Address.Country country = new Person.Address.Country();
    country.setName("Foo");
    country.setCountryCode("DE");
    p.getAddress().setCountry(country);
    BiPredicate<PropertyPath, Neo4jPersistentProperty> predicate = create2LevelProjectingPredicate();
    List<Person> projections = neo4jTemplate.saveAllAs(Collections.singletonList(p), predicate);
    assertThat(projections).hasSize(1).first().satisfies(projection -> {
        assertThat(projection.getAddress().getStreet()).isEqualTo("Single Trail");
        assertThat(projection.getAddress().getCountry().getName()).isEqualTo("Foo");
    });
    p = neo4jTemplate.findById(p.getId(), Person.class).get();
    assertThat(p.getFirstName()).isEqualTo("Michael");
    assertThat(p.getLastName()).isEqualTo("Simons");
    assertThat(p.getAddress().getCity()).isEqualTo("Aachen");
    assertThat(p.getAddress().getStreet()).isEqualTo("Single Trail");
    assertThat(p.getAddress().getCountry().getName()).isEqualTo("Foo");
}
Also used : PropertyPath(org.springframework.data.mapping.PropertyPath) Person(org.springframework.data.neo4j.integration.shared.common.Person) Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty) Neo4jIntegrationTest(org.springframework.data.neo4j.test.Neo4jIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 2 with Neo4jPersistentProperty

use of org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty in project spring-data-neo4j by spring-projects.

the class Neo4jTemplateIT method saveAsWithDynamicProjectionOnSecondLevelShouldWork.

// GH-2420
@Test
void saveAsWithDynamicProjectionOnSecondLevelShouldWork() {
    Person p = neo4jTemplate.findOne("MATCH (p:Person {lastName: $lastName})-[r:LIVES_AT]-(a:Address) RETURN p, collect(r), collect(a)", Collections.singletonMap("lastName", "Siemons"), Person.class).get();
    p.getAddress().setCity("Braunschweig");
    p.getAddress().setStreet("Single Trail");
    Person.Address.Country country = new Person.Address.Country();
    country.setName("Foo");
    country.setCountryCode("DE");
    p.getAddress().setCountry(country);
    BiPredicate<PropertyPath, Neo4jPersistentProperty> predicate = create2LevelProjectingPredicate();
    Person projection = neo4jTemplate.saveAs(p, predicate);
    assertThat(projection.getAddress().getStreet()).isEqualTo("Single Trail");
    assertThat(projection.getAddress().getCountry().getName()).isEqualTo("Foo");
    p = neo4jTemplate.findById(p.getId(), Person.class).get();
    assertThat(p.getAddress().getCity()).isEqualTo("Aachen");
    assertThat(p.getAddress().getStreet()).isEqualTo("Single Trail");
    assertThat(p.getAddress().getCountry().getName()).isEqualTo("Foo");
}
Also used : PropertyPath(org.springframework.data.mapping.PropertyPath) Person(org.springframework.data.neo4j.integration.shared.common.Person) Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty) Neo4jIntegrationTest(org.springframework.data.neo4j.test.Neo4jIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 3 with Neo4jPersistentProperty

use of org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty in project spring-data-neo4j by spring-projects.

the class IdPopulator method populateIfNecessary.

Object populateIfNecessary(Object entity) {
    Assert.notNull(entity, "Entity may not be null!");
    Neo4jPersistentEntity<?> nodeDescription = neo4jMappingContext.getRequiredPersistentEntity(entity.getClass());
    IdDescription idDescription = nodeDescription.getIdDescription();
    if (idDescription == null) {
        if (nodeDescription.isRelationshipPropertiesEntity()) {
            return entity;
        } else {
            throw new IllegalStateException("Cannot persist implicit entity due to missing id property on " + nodeDescription.getUnderlyingClass() + ".");
        }
    }
    // Filter in two steps to avoid unnecessary object creation.
    if (!idDescription.isExternallyGeneratedId()) {
        return entity;
    }
    PersistentPropertyAccessor<?> propertyAccessor = nodeDescription.getPropertyAccessor(entity);
    Neo4jPersistentProperty idProperty = nodeDescription.getRequiredIdProperty();
    // Check existing ID
    if (propertyAccessor.getProperty(idProperty) != null) {
        return entity;
    }
    IdGenerator<?> idGenerator;
    // Get or create the shared generator
    // Ref has precedence over class
    Optional<String> optionalIdGeneratorRef = idDescription.getIdGeneratorRef();
    if (optionalIdGeneratorRef.isPresent()) {
        idGenerator = neo4jMappingContext.getIdGenerator(optionalIdGeneratorRef.get()).orElseThrow(() -> new IllegalStateException("Id generator named " + optionalIdGeneratorRef.get() + " not found!"));
    } else {
        idGenerator = neo4jMappingContext.getOrCreateIdGeneratorOfType(idDescription.getIdGeneratorClass().orElseThrow(() -> new IllegalStateException("Neither generator reference nor generator class configured.")));
    }
    propertyAccessor.setProperty(idProperty, idGenerator.generateId(nodeDescription.getPrimaryLabel(), entity));
    return propertyAccessor.getBean();
}
Also used : IdDescription(org.springframework.data.neo4j.core.mapping.IdDescription) Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty)

Example 4 with Neo4jPersistentProperty

use of org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty in project spring-data-neo4j by spring-projects.

the class SimpleReactiveNeo4jRepository method delete.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.repository.reactive.ReactiveCrudRepository#delete(java.lang.Object)
	 */
@Override
@Transactional
public Mono<Void> delete(T entity) {
    Assert.notNull(entity, "The given entity must not be null!");
    ID id = this.entityInformation.getId(entity);
    Assert.notNull(id, "Cannot delete individual nodes without an id.");
    if (entityMetaData.hasVersionProperty()) {
        Neo4jPersistentProperty versionProperty = entityMetaData.getRequiredVersionProperty();
        Object versionValue = entityMetaData.getPropertyAccessor(entity).getProperty(versionProperty);
        return this.neo4jOperations.deleteByIdWithVersion(id, this.entityInformation.getJavaType(), versionProperty, versionValue);
    } else {
        return this.deleteById(id);
    }
}
Also used : Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with Neo4jPersistentProperty

use of org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty in project spring-data-neo4j by spring-projects.

the class Gh2168IT method assertWriteAndReadConversionForProperty.

private void assertWriteAndReadConversionForProperty(Neo4jPersistentEntity<?> entity, String propertyName, DomainObjectRepository repository, Driver driver, BookmarkCapture bookmarkCapture) {
    Neo4jPersistentProperty property = entity.getPersistentProperty(propertyName);
    PersistentPropertyAccessor<DomainObject> propertyAccessor = entity.getPropertyAccessor(new DomainObject());
    propertyAccessor.setProperty(property, new UnrelatedObject(true, 4711L));
    DomainObject domainObject = repository.save(propertyAccessor.getBean());
    try (Session session = driver.session(bookmarkCapture.createSessionConfig())) {
        Node node = session.run("MATCH (n:DomainObject {id: $id}) RETURN n", Collections.singletonMap("id", domainObject.getId())).single().get(0).asNode();
        assertThat(node.get(propertyName).asString()).isEqualTo("true;4711");
    }
    domainObject = repository.findById(domainObject.getId()).get();
    UnrelatedObject unrelatedObject = (UnrelatedObject) entity.getPropertyAccessor(domainObject).getProperty(property);
    assertThat(unrelatedObject).satisfies(t -> {
        assertThat(t.isABooleanValue()).isTrue();
        assertThat(t.getALongValue()).isEqualTo(4711L);
    });
}
Also used : Node(org.neo4j.driver.types.Node) Neo4jPersistentProperty(org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty) Session(org.neo4j.driver.Session)

Aggregations

Neo4jPersistentProperty (org.springframework.data.neo4j.core.mapping.Neo4jPersistentProperty)7 Test (org.junit.jupiter.api.Test)2 PropertyPath (org.springframework.data.mapping.PropertyPath)2 Person (org.springframework.data.neo4j.integration.shared.common.Person)2 Neo4jIntegrationTest (org.springframework.data.neo4j.test.Neo4jIntegrationTest)2 Transactional (org.springframework.transaction.annotation.Transactional)2 ElementType (java.lang.annotation.ElementType)1 Inherited (java.lang.annotation.Inherited)1 Retention (java.lang.annotation.Retention)1 RetentionPolicy (java.lang.annotation.RetentionPolicy)1 Target (java.lang.annotation.Target)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1 UnaryOperator (java.util.function.UnaryOperator)1 Collectors (java.util.stream.Collectors)1