Exemplo n.º 1
0
  /**
   * Cascades the detach operation.
   *
   * @param entityManager the entity manager
   * @since 2.0.0
   */
  public void cascadeDetach(EntityManagerImpl entityManager) {
    this.status = Status.DETACHED;

    ManagedInstance.LOG.debug("Cascading detach on {0}", this);

    for (final AssociationMappingImpl<?, ?, ?> association :
        this.type.getAssociationsDetachable()) {

      // if the association a collection attribute then we will cascade to each element
      if (association instanceof PluralAssociationMappingImpl) {
        final PluralAssociationMappingImpl<?, ?, ?> mapping =
            (PluralAssociationMappingImpl<?, ?, ?>) association;

        final Collection<?> collection;
        if (mapping.getAttribute().getCollectionType() == CollectionType.MAP) {
          collection = ((Map<?, ?>) mapping.get(this.instance)).values();
        } else {
          // extract the collection
          collection = (Collection<?>) mapping.get(this.instance);
        }

        // cascade to each element in the collection
        if (collection instanceof List) {
          final List<?> list = (List<?>) collection;
          for (int i = 0; i < list.size(); i++) {
            entityManager.detach(list.get(i));
          }
        } else {
          for (final Object element : collection) {
            entityManager.detach(element);
          }
        }
      } else {
        final SingularAssociationMappingImpl<?, ?> mapping =
            (SingularAssociationMappingImpl<?, ?>) association;
        final Object associate = mapping.get(this.instance);

        entityManager.detach(associate);
      }
    }
  }