use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class LoadOneDelegate method load.
public <T, ID extends Serializable> T load(Class<T> type, ID id, int depth) {
ClassInfo classInfo = session.metaData().classInfo(type.getName());
if (classInfo == null) {
throw new IllegalArgumentException(type + " is not a managed entity.");
}
final FieldInfo primaryIndexField = classInfo.primaryIndexField();
if (primaryIndexField != null && !primaryIndexField.isTypeOf(id.getClass())) {
throw new IllegalArgumentException("Supplied id does not match primary index type on supplied class " + type.getName());
}
if (primaryIndexField == null && !(id instanceof Long)) {
throw new IllegalArgumentException("Supplied id must be of type Long (native graph id) when supplied class " + "does not have primary id " + type.getName());
}
Optional<String> labelsOrType = session.determineLabelsOrTypeForLoading(type);
if (!labelsOrType.isPresent()) {
logger.warn("Unable to find database label for entity " + type.getName() + " : no results will be returned. Make sure the class is registered, " + "and not abstract without @NodeEntity annotation");
return null;
}
QueryStatements<ID> queryStatements = session.queryStatementsFor(type, depth);
PagingAndSortingQuery qry = queryStatements.findOneByType(labelsOrType.get(), convertIfNeeded(classInfo, id), depth);
GraphModelRequest request = new DefaultGraphModelRequest(qry.getStatement(), qry.getParameters());
return session.doInTransaction(() -> {
try (Response<GraphModel> response = session.requestHandler().execute(request)) {
new GraphRowModelMapper(session.metaData(), session.context(), session.getEntityInstantiator()).map(type, response);
return lookup(type, id);
}
}, Transaction.Type.READ_ONLY);
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class SaveEventDelegate method children.
// for a given object parent, returns a list of objects referenced by this parent, i.e. its children
// at this stage, these children may or may not represent related nodes in the graph to the parent node
// We'll figure that out later.
private List<Object> children(Object parent) {
List<Object> children = new ArrayList<>();
ClassInfo parentClassInfo = this.session.metaData().classInfo(parent);
if (parentClassInfo != null) {
for (FieldInfo reader : parentClassInfo.relationshipFields()) {
Object reference = reader.read(parent);
if (reference != null) {
CollectionUtils.iterableOf(reference).forEach(children::add);
}
}
}
return children;
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class SessionDelegate method resolvePropertyName.
private String resolvePropertyName(Class entityType, String propertyName) {
ClassInfo classInfo = session.metaData().classInfo(entityType.getName());
FieldInfo fieldInfo = classInfo.propertyFieldByName(propertyName);
if (fieldInfo != null && fieldInfo.getAnnotations() != null) {
AnnotationInfo annotation = fieldInfo.getAnnotations().get(Property.class);
if (annotation != null) {
return annotation.get(Property.NAME, propertyName);
}
}
return propertyName;
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class RelationshipDeleteStatements method delete.
@Override
public CypherQuery delete(Long id, Object object, ClassInfo classInfo) {
FieldInfo versionField = classInfo.getVersionField();
Long version = (Long) versionField.read(object);
OptimisticLockingConfig optimisticLockingConfig = new OptimisticLockingConfig(1, classInfo.staticLabels().toArray(new String[] {}), versionField.property());
Map<String, Object> params = new HashMap<>();
params.put("id", id);
params.put("version", version);
params.put("type", "rel");
return new DefaultRowModelRequest("MATCH (n)-[r0]->() " + " WHERE ID(r0) = $id AND r0.`" + versionField.property() + "` = $version " + "SET " + " r0.`" + versionField.property() + "` = r0.`" + versionField.property() + "` + 1 " + "WITH r0 " + " WHERE r0.`" + versionField.property() + "` = $version + 1 " + "DELETE r0 " + // Use DISTINCT because node may have multiple relationships
"RETURN DISTINCT ID(r0) AS id", params, optimisticLockingConfig);
}
use of org.neo4j.ogm.metadata.FieldInfo in project neo4j-ogm by neo4j.
the class AnnotatedFieldNonAnnotatedSetterAndNonSetterTest method shouldPreferAnnotatedFieldOverNonAnnotatedSetterAndNonSetter.
@Test
public void shouldPreferAnnotatedFieldOverNonAnnotatedSetterAndNonSetter() {
ClassInfo classInfo = this.domainInfo.getClass(End.class.getName());
RelEntity relEntity = new RelEntity();
Set<RelEntity> parameter = new HashSet();
parameter.addAll(Arrays.asList(relEntity));
FieldInfo objectAccess = EntityAccessManager.getRelationalWriter(classInfo, "REL_ENTITY_TYPE", Relationship.Direction.INCOMING, relEntity);
assertThat(objectAccess).as("The resultant object accessor shouldn't be null").isNotNull();
assertThat(objectAccess instanceof FieldInfo).as("The access mechanism should be via the field").isTrue();
End end = new End();
objectAccess.write(end, parameter);
assertThat(parameter).isEqualTo(end.getRelEntities());
}
Aggregations