@Override
  protected void deleteEntity(PersistentEntity pe, Object obj) {
    EntityAccess entityAccess = createEntityAccess(pe, obj);
    if (cancelDelete(pe, entityAccess)) {
      return;
    }

    for (Association association : pe.getAssociations()) {
      if (association.isOwningSide() && association.doesCascade(CascadeType.REMOVE)) {
        log.debug("cascading delete for property " + association.getName());

        GraphPersistentEntity otherPersistentEntity =
            (GraphPersistentEntity) association.getAssociatedEntity();
        Object otherSideValue = entityAccess.getProperty(association.getName());
        if (association instanceof ToOne) {
          deleteEntity(otherPersistentEntity, otherSideValue);
        } else {
          deleteEntities(otherPersistentEntity, (Iterable) otherSideValue);
        }
      }
    }

    getCypherEngine()
        .execute(
            String.format(
                "MATCH (n:%s) WHERE n.__id__={id} OPTIONAL MATCH (n)-[r]-() DELETE r,n",
                ((GraphPersistentEntity) pe).getLabel()),
            Collections.singletonMap("id", entityAccess.getIdentifier()));

    firePostDeleteEvent(pe, entityAccess);
  }
  @Override
  protected void setManyToMany(
      PersistentEntity persistentEntity,
      Object obj,
      DBObject nativeEntry,
      ManyToMany manyToMany,
      Collection associatedObjects,
      Map<Association, List<Serializable>> toManyKeys) {

    List ids = new ArrayList();
    if (associatedObjects != null) {
      for (Object o : associatedObjects) {
        if (o == null) {
          ids.add(null);
        } else {
          PersistentEntity childPersistentEntity =
              getMappingContext().getPersistentEntity(o.getClass().getName());
          EntityAccess entityAccess = createEntityAccess(childPersistentEntity, o);
          ids.add(entityAccess.getIdentifier());
        }
      }
    }

    nativeEntry.put(manyToMany.getName() + "_$$manyToManyIds", ids);
  }
  @Override
  protected void deleteEntities(
      PersistentEntity pe, @SuppressWarnings("rawtypes") Iterable objects) {
    List<EntityAccess> entityAccesses = new ArrayList<EntityAccess>();
    List<Object> ids = new ArrayList<Object>();
    Map<PersistentEntity, Collection<Object>> cascades =
        new HashMap<PersistentEntity, Collection<Object>>();

    for (Object obj : objects) {
      EntityAccess entityAccess = createEntityAccess(pe, obj);
      if (cancelDelete(pe, entityAccess)) {
        return;
      }
      entityAccesses.add(entityAccess);
      ids.add(entityAccess.getIdentifier());

      // populate cascades
      for (Association association : pe.getAssociations()) {
        Object property = entityAccess.getProperty(association.getName());
        if (association.isOwningSide()
            && association.doesCascade(CascadeType.REMOVE)
            && (property != null)) {

          PersistentEntity associatedEntity = association.getAssociatedEntity();

          Collection<Object> cascadesForPersistentEntity = cascades.get(associatedEntity);
          if (cascadesForPersistentEntity == null) {
            cascadesForPersistentEntity = new ArrayList<Object>();
            cascades.put(associatedEntity, cascadesForPersistentEntity);
          }

          if (association instanceof ToOne) {
            cascadesForPersistentEntity.add(property);
          } else {
            cascadesForPersistentEntity.addAll((Collection<?>) property);
          }
        }
      }
    }

    for (Map.Entry<PersistentEntity, Collection<Object>> entry : cascades.entrySet()) {
      deleteEntities(entry.getKey(), entry.getValue());
    }

    getCypherEngine()
        .execute(
            String.format(
                "MATCH (n:%s) WHERE n.__id__ in {ids} OPTIONAL MATCH (n)-[r]-() DELETE r,n",
                ((GraphPersistentEntity) pe).getLabel()),
            Collections.singletonMap("ids", ids));

    for (EntityAccess entityAccess : entityAccesses) {
      firePostDeleteEvent(pe, entityAccess);
    }
  }
  @Override
  protected Serializable persistEntity(PersistentEntity pe, Object obj) {
    if ((obj == null) || (getSession().containsPersistingInstance(obj))) {
      return null;
    }

    EntityAccess entityAccess = createEntityAccess(pe, obj);
    if (getMappingContext().getProxyFactory().isProxy(obj)) {
      return (Serializable) entityAccess.getIdentifier();
    }

    getSession().addPersistingInstance(obj);

    // cancel operation if vetoed
    boolean isUpdate = entityAccess.getIdentifier() != null;
    if (isUpdate) {
      if (cancelUpdate(pe, entityAccess)) {
        return null;
      }
      getSession()
          .addPendingUpdate(
              new NodePendingUpdate(entityAccess, getCypherEngine(), getMappingContext()));
      persistAssociationsOfEntity(pe, entityAccess, true);
      firePostUpdateEvent(pe, entityAccess);

    } else {
      if (cancelInsert(pe, entityAccess)) {
        return null;
      }
      getSession()
          .addPendingInsert(
              new NodePendingInsert(
                  getSession().getDatastore().nextIdForType(pe),
                  entityAccess,
                  getCypherEngine(),
                  getMappingContext()));
      persistAssociationsOfEntity(pe, entityAccess, false);
      firePostInsertEvent(pe, entityAccess);
    }

    return (Serializable) entityAccess.getIdentifier();
  }
  @Override
  public void run() {

    //        validateNonExistingRelationship("execution");

    List params = new ArrayList();
    params.add(getEntityAccess().getIdentifier());
    params.add(target.getIdentifier());

    String labelsFrom = ((GraphPersistentEntity) getEntity()).getLabelsAsString();
    String labelsTo = ((GraphPersistentEntity) target.getPersistentEntity()).getLabelsAsString();
    String cypher =
        String.format(
            "MATCH (from%s {__id__:{1}}), (to%s {__id__:{2}}) MERGE (from)-[:%s]->(to)",
            labelsFrom, labelsTo, relType);
    cypherEngine.execute(cypher, params);
  }
  @Override
  public boolean isDirty(Object instance, Object entry) {
    if (super.isDirty(instance, entry)) {
      return true;
    }

    DBObject dbo = (DBObject) entry;
    PersistentEntity entity = getPersistentEntity();

    EntityAccess entityAccess = createEntityAccess(entity, instance, dbo);

    DBObject cached =
        (DBObject)
            ((SessionImplementor<?>) getSession())
                .getCachedEntry(entity, (Serializable) entityAccess.getIdentifier(), true);

    return !dbo.equals(cached);
  }