use of org.neo4j.ogm.context.TransientRelationship in project neo4j-ogm by neo4j.
the class RequestExecutor method buildRegisteredTransientRelationshipIndex.
/**
* Append {@link TransientRelationship} of {@link CompileContext} to an index.
*
* @param context the compile context
* @return an index of {@link TransientRelationship}
*/
private Map<Long, TransientRelationship> buildRegisteredTransientRelationshipIndex(CompileContext context) {
final Map<Long, TransientRelationship> transientRelationshipIndex = new HashMap<>();
for (Object obj : context.registry()) {
if (TransientRelationship.class.isAssignableFrom(obj.getClass())) {
TransientRelationship transientRelationship = (TransientRelationship) obj;
transientRelationshipIndex.put(transientRelationship.getRef(), transientRelationship);
}
}
return transientRelationshipIndex;
}
use of org.neo4j.ogm.context.TransientRelationship in project neo4j-ogm by neo4j.
the class RequestExecutor method updateRelationships.
/**
* Update the mapping context with new relationships created in a request.
*
* @param context the compile context
* @param relRefMappings mapping of relationship reference used in the compile context and the relationship id from the database
*/
private void updateRelationships(CompileContext context, List<ReferenceMapping> relRefMappings) {
final Map<Long, TransientRelationship> registeredTransientRelationshipIndex = buildRegisteredTransientRelationshipIndex(context);
for (ReferenceMapping referenceMapping : relRefMappings) {
if (registeredTransientRelationshipIndex.containsKey(referenceMapping.ref)) {
TransientRelationship transientRelationship = registeredTransientRelationshipIndex.get(referenceMapping.ref);
boolean isRelationshipEntity = session.context().getRelationshipEntity(referenceMapping.id) != null;
MappedRelationship mappedRelationship = new MappedRelationship(context.getId(transientRelationship.getSrc()), transientRelationship.getRel(), context.getId(transientRelationship.getTgt()), isRelationshipEntity ? referenceMapping.id : null, transientRelationship.getSrcClass(), transientRelationship.getTgtClass());
session.context().addRelationship(mappedRelationship);
}
}
}
use of org.neo4j.ogm.context.TransientRelationship in project neo4j-ogm by neo4j.
the class RequestExecutor method updateNodeEntities.
/**
* Update the mapping context with entity ids for new/existing nodes created or updated in the request.
*
* @param context the compile context
* @param entityRefMappings mapping of entity reference used in the compile context and the entity id from the database
*/
private void updateNodeEntities(CompileContext context, List<ReferenceMapping> entityRefMappings) {
// Ensures the last saved version of existing nodes is current in the cache
for (Object obj : context.registry()) {
if (!(obj instanceof TransientRelationship)) {
ClassInfo classInfo = session.metaData().classInfo(obj);
if (!classInfo.isRelationshipEntity()) {
session.context().optionalNativeId(obj).filter(id -> id >= 0).ifPresent(id -> {
LOGGER.debug("updating existing node id: {}, {}", id, obj);
registerEntity(session.context(), classInfo, id, obj);
});
}
}
}
// Ensures newly created nodes are current in the cache and also assigns their new graph ids
for (ReferenceMapping referenceMapping : entityRefMappings) {
// by a reference mapping will always have identical 'ref' and 'id' values.
if (!(referenceMapping.ref.equals(referenceMapping.id))) {
Object newEntity = context.getNewObject(referenceMapping.ref);
LOGGER.debug("creating new node id: {}, {}, {}", referenceMapping.ref, referenceMapping.id, newEntity);
initialiseNewEntity(referenceMapping.id, newEntity);
}
}
}
Aggregations