/** * Cascades the remove operation * * @param entityManager the entity manager * @param processed registry of processed entities * @param instances the managed instances * @since 2.0.0 */ public void cascadeRemove( EntityManagerImpl entityManager, ArrayList<Object> processed, LinkedList<ManagedInstance<?>> instances) { ManagedInstance.LOG.debug("Cascading remove on {0}", this); for (final AssociationMappingImpl<?, ?, ?> association : this.type.getAssociationsRemovable()) { // if the association a collection attribute then we will cascade to each element if (association instanceof PluralAssociationMappingImpl) { final PluralAssociationMappingImpl<?, ?, ?> mapping = (PluralAssociationMappingImpl<?, ?, ?>) association; // extract the collection final Collection<?> collection; if (mapping.getAttribute().getCollectionType() == CollectionType.MAP) { collection = ((Map<?, ?>) mapping.get(this.instance)).values(); } else { 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.removeImpl(list.get(i), processed, instances); } } else { for (final Object element : collection) { entityManager.removeImpl(element, processed, instances); } } } else { final SingularAssociationMappingImpl<?, ?> mapping = (SingularAssociationMappingImpl<?, ?>) association; final Object associate = mapping.get(this.instance); if (associate != null) { entityManager.removeImpl(associate, processed, instances); } } } }
/** * 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); } } }
/** * Processes the associations. * * @since 2.0.0 */ public void processJoinedMappings() { ManagedInstance.LOG.debug("Post processing associations for instance {0}", this); final HashSet<String> _joinsLoaded = this.joinsLoaded; for (final PluralMappingEx<?, ?, ?> mapping : this.type.getMappingsPlural()) { final HashSet<String> joinsLoaded2 = _joinsLoaded; if (!joinsLoaded2.contains(mapping.getPath())) { if (mapping.isEager()) { mapping.load(this); } else { mapping.setLazy(this); } } } final X _instance = this.instance; final EntityManagerImpl entityManager = this.session.getEntityManager(); for (final SingularAssociationMappingImpl<?, ?> mapping : this.type.getAssociationsSingular()) { if (mapping.isEager()) { if (!_joinsLoaded.contains(mapping.getPath())) { mapping.initialize(this); } else { final Object associate = mapping.get(_instance); if (associate instanceof EnhancedInstance) { final EnhancedInstance enhancedInstance = (EnhancedInstance) associate; if (!enhancedInstance.__enhanced__$$__isInitialized()) { final ManagedInstance<?> associateManagedInstance = enhancedInstance.__enhanced__$$__getManagedInstance(); entityManager.find( associateManagedInstance.getType().getJavaType(), associateManagedInstance.getId().getId()); } } } } } }
/** * Cascades the persist operation. * * @param entityManager the entity manager * @param processed registry of processed entities * @param instances the managed instances * @return true if an implicit flush is required, false otherwise * @since 2.0.0 */ public boolean cascadePersist( EntityManagerImpl entityManager, ArrayList<Object> processed, LinkedList<ManagedInstance<?>> instances) { ManagedInstance.LOG.debug("Cascading persist on {0}", this); boolean requiresFlush = false; for (final AssociationMappingImpl<?, ?, ?> association : this.type.getAssociationsPersistable()) { // if the association a collection attribute then we will cascade to each element if (association instanceof PluralAssociationMappingImpl) { final PluralAssociationMappingImpl<?, ?, ?> mapping = (PluralAssociationMappingImpl<?, ?, ?>) association; switch (mapping.getAttribute().getCollectionType()) { case MAP: // extract the map final Map<?, ?> map = (Map<?, ?>) mapping.get(this.instance); // cascade to each element in the map for (final Object element : map.values()) { requiresFlush |= entityManager.persistImpl(element, processed, instances); } break; case LIST: // extract the list final List<?> list = (List<?>) mapping.get(this.instance); // cascade to each element in the list for (int i = 0; i < list.size(); i++) { requiresFlush |= entityManager.persistImpl(list.get(i), processed, instances); } break; default: // extract the collection final Collection<?> collection = (Collection<?>) mapping.get(this.instance); // cascade to each element in the collection if (collection instanceof List) { final List<?> castedList = (List<?>) collection; for (int i = 0; i < castedList.size(); i++) { requiresFlush |= entityManager.persistImpl(castedList.get(i), processed, instances); } } else { for (final Object element : collection) { requiresFlush |= entityManager.persistImpl(element, processed, instances); } } break; } } else { final SingularAssociationMappingImpl<?, ?> mapping = (SingularAssociationMappingImpl<?, ?>) association; final Object associate = mapping.get(this.instance); if (associate != null) { requiresFlush |= entityManager.persistImpl(associate, processed, instances); } } } return requiresFlush; }