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");
}
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");
}
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();
}
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);
}
}
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);
});
}
Aggregations