use of org.neo4j.ogm.metadata.ClassInfo 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.ClassInfo 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.ClassInfo in project neo4j-ogm by neo4j.
the class SaveEventDelegate method mapInstance.
// creates a MappedRelationship between the parent object and the reference. In the case that the reference
// object is a RE, the relationship is created from the start node and the end node of the RE.
// a MappedRelationship therefore represents a directed edge between two nodes in the graph.
private void mapInstance(Set<MappedRelationship> mappedRelationships, ClassInfo parentInfo, long parentId, FieldInfo reader, Object reference) {
String type = reader.relationshipType();
Direction direction = reader.relationshipDirection();
ClassInfo referenceInfo = this.session.metaData().classInfo(reference);
if (referenceInfo == null) {
return;
}
if (referenceInfo.isRelationshipEntity()) {
// The relationship entity might just get created and therefore we must be careful not to
// trigger the creation of it's id place holder, otherwise we can't check wether it's new or not.
Optional<Long> optionalReferenceId = session.context().optionalNativeId(reference);
// graph relationship is transitive across the RE domain object
Object startNode = referenceInfo.getStartNodeReader().read(reference);
ClassInfo startNodeInfo = this.session.metaData().classInfo(startNode);
Long startNodeId = session.context().nativeId(startNode);
Object endNode = referenceInfo.getEndNodeReader().read(reference);
ClassInfo endNodeInfo = this.session.metaData().classInfo(endNode);
Long endNodeId = session.context().nativeId(endNode);
MappedRelationship edge = new MappedRelationship(startNodeId, type, endNodeId, optionalReferenceId.orElse(null), startNodeInfo.getUnderlyingClass(), endNodeInfo.getUnderlyingClass());
mappedRelationships.add(edge);
} else {
// We assume the existence of the reference here
Long referenceId = session.context().nativeId(reference);
if (direction == Direction.OUTGOING) {
MappedRelationship edge = new MappedRelationship(parentId, type, referenceId, null, parentInfo.getUnderlyingClass(), referenceInfo.getUnderlyingClass());
mappedRelationships.add(edge);
} else {
MappedRelationship edge = new MappedRelationship(referenceId, type, parentId, null, referenceInfo.getUnderlyingClass(), parentInfo.getUnderlyingClass());
mappedRelationships.add(edge);
}
}
}
use of org.neo4j.ogm.metadata.ClassInfo 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.ClassInfo in project neo4j-ogm by neo4j.
the class RequestExecutor method initialiseNewEntity.
/**
* Register entities in the {@link MappingContext}
*
* @param persisted entity created as part of the request
*/
private void initialiseNewEntity(Long identity, Object persisted) {
MappingContext mappingContext = session.context();
Transaction tx = session.getTransaction();
if (persisted != null) {
// it will be null if the variable represents a simple relationship.
// set the id field of the newly created domain object
EntityUtils.setIdentity(persisted, identity, session.metaData());
ClassInfo classInfo = session.metaData().classInfo(persisted);
if (tx != null) {
((AbstractTransaction) tx).registerNew(persisted);
}
registerEntity(mappingContext, classInfo, identity, persisted);
}
}
Aggregations