/// <summary> /// Cascade an action from the parent entity instance to all its children. This /// form is typically called from within cascade actions. /// </summary> /// <param name="persister">The parent's entity persister </param> /// <param name="parent">The parent reference. </param> /// <param name="anything"> /// Typically some form of cascade-local cache /// which is specific to each CascadingAction type /// </param> /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param> public async Task CascadeOnAsync(IEntityPersister persister, object parent, object anything, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (persister.HasCascades || action.RequiresNoCascadeChecking) { log.Info("processing cascade " + action + " for: " + persister.EntityName); IType[] types = persister.PropertyTypes; CascadeStyle[] cascadeStyles = persister.PropertyCascadeStyles; bool hasUninitializedLazyProperties = persister.HasUninitializedLazyProperties(parent); for (int i = 0; i < types.Length; i++) { CascadeStyle style = cascadeStyles[i]; string propertyName = persister.PropertyNames[i]; if (hasUninitializedLazyProperties && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty) { //do nothing to avoid a lazy property initialization continue; } if (style.DoCascade(action)) { await(CascadePropertyAsync(parent, persister.GetPropertyValue(parent, i), types[i], style, propertyName, anything, false, cancellationToken)).ConfigureAwait(false); } else if (action.RequiresNoCascadeChecking) { await(action.NoCascadeAsync(eventSource, persister.GetPropertyValue(parent, i), parent, persister, i, cancellationToken)).ConfigureAwait(false); } } log.Info("done processing cascade " + action + " for: " + persister.EntityName); } }
/// <summary> /// Cascade an action from the parent entity instance to all its children. This /// form is typically called from within cascade actions. /// </summary> /// <param name="persister">The parent's entity persister </param> /// <param name="parent">The parent reference. </param> /// <param name="anything"> /// Typically some form of cascade-local cache /// which is specific to each CascadingAction type /// </param> public void CascadeOn(IEntityPersister persister, object parent, object anything) { if (persister.HasCascades || action.RequiresNoCascadeChecking) { log.Info("processing cascade {0} for: {1}", action, persister.EntityName); IType[] types = persister.PropertyTypes; CascadeStyle[] cascadeStyles = persister.PropertyCascadeStyles; bool hasUninitializedLazyProperties = persister.HasUninitializedLazyProperties(parent); for (int i = 0; i < types.Length; i++) { CascadeStyle style = cascadeStyles[i]; string propertyName = persister.PropertyNames[i]; if (hasUninitializedLazyProperties && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty) { //do nothing to avoid a lazy property initialization continue; } if (style.DoCascade(action)) { CascadeProperty(parent, persister.GetPropertyValue(parent, i), types[i], style, propertyName, anything, false); } else if (action.RequiresNoCascadeChecking) { action.NoCascade(eventSource, persister.GetPropertyValue(parent, i), parent, persister, i); } } log.Info("done processing cascade {0} for: {1}", action, persister.EntityName); } }
protected internal object GetReferenceValue(object value, ISessionImplementor session) { if (value == null) { return(null); } else if (IsReferenceToPrimaryKey) { return(ForeignKeys.GetEntityIdentifierIfNotUnsaved(GetAssociatedEntityName(), value, session)); //tolerates nulls } else { IEntityPersister entityPersister = session.Factory.GetEntityPersister(GetAssociatedEntityName()); object propertyValue = entityPersister.GetPropertyValue(value, uniqueKeyPropertyName); // We now have the value of the property-ref we reference. However, // we need to dig a little deeper, as that property might also be // an entity type, in which case we need to resolve its identitifier IType type = entityPersister.GetPropertyType(uniqueKeyPropertyName); if (type.IsEntityType) { propertyValue = ((EntityType)type).GetReferenceValue(propertyValue, session); } return(propertyValue); } }
protected internal async Task <object> GetReferenceValueAsync(object value, ISessionImplementor session, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); if (value == null) { return(null); } else if (IsReferenceToPrimaryKey) { return(await(ForeignKeys.GetEntityIdentifierIfNotUnsavedAsync(GetAssociatedEntityName(), value, session, cancellationToken)).ConfigureAwait(false)); //tolerates nulls } else { IEntityPersister entityPersister = session.Factory.GetEntityPersister(GetAssociatedEntityName()); object propertyValue = entityPersister.GetPropertyValue(value, uniqueKeyPropertyName); // We now have the value of the property-ref we reference. However, // we need to dig a little deeper, as that property might also be // an entity type, in which case we need to resolve its identitifier IType type = entityPersister.GetPropertyType(uniqueKeyPropertyName); if (type.IsEntityType) { propertyValue = await(((EntityType)type).GetReferenceValueAsync(propertyValue, session, cancellationToken)).ConfigureAwait(false); } return(propertyValue); } }
public void Initialize(object key, ISessionImplementor session, IEntityPersister OwnerEntityPersister, object owner) { if (log.IsDebugEnabled) { log.Debug(string.Format("initializing collection: {0} using named query: {1}", persister.Role, queryName)); } //TODO: is there a more elegant way than downcasting? AbstractQueryImpl query = (AbstractQueryImpl)session.GetNamedSQLQuery(queryName); if (query.NamedParameters.Length > 0) { //این قسمت به دلیل حل نمودن مشکل //not all named parameters have been set //اضافه شده است #region MyCode try { foreach (string parameter in query.NamedParameters) { string alias = parameter.Split('.')[0]; int length = parameter.Split('.').Length; if (session.EnabledFilters.ContainsKey(alias)) { query.SetParameter(parameter, session.GetFilterParameterValue(parameter)); } else { if (length > 1) { string paramName = parameter.Split('.')[1]; object value = OwnerEntityPersister.GetPropertyValue(owner, paramName, EntityMode.Poco); if (value != null) { query.SetParameter(parameter, value); } } else { query.SetParameter(parameter, key, persister.KeyType); } } } } catch (Exception ex) { throw new Exception(String.Format("{0}: {1}", "NamedQueryCollectionInitializer.Initialize", "خطا در کد نوشته شده توسط صفری نیا"), ex); } #endregion } query.SetCollectionKey(key).SetFlushMode(FlushMode.Never).List(); }
private async Task <object> MergeTransientEntityAsync(object entity, string entityName, object requestedId, IEventSource source, IDictionary copyCache, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); IEntityPersister persister = source.GetEntityPersister(entityName, entity); object id = persister.HasIdentifierProperty ? persister.GetIdentifier(entity) : null; object copy = null; if (copyCache.Contains(entity)) { copy = copyCache[entity]; persister.SetIdentifier(copy, id); } else { copy = source.Instantiate(persister, id); ((EventCache)copyCache).Add(entity, copy, true); // before cascade! } // cascade first, so that all unsaved objects get their // copy created before we actually copy //cascadeOnMerge(event, persister, entity, copyCache, Cascades.CASCADE_BEFORE_MERGE); await(base.CascadeBeforeSaveAsync(source, persister, entity, copyCache, cancellationToken)).ConfigureAwait(false); await(CopyValuesAsync(persister, entity, copy, source, copyCache, ForeignKeyDirection.ForeignKeyFromParent, cancellationToken)).ConfigureAwait(false); try { // try saving; check for non-nullable properties that are null or transient entities before saving await(this.SaveTransientEntityAsync(copy, entityName, requestedId, source, copyCache, cancellationToken)).ConfigureAwait(false); } catch (PropertyValueException ex) { string propertyName = ex.PropertyName; object propertyFromCopy = persister.GetPropertyValue(copy, propertyName); object propertyFromEntity = persister.GetPropertyValue(entity, propertyName); IType propertyType = persister.GetPropertyType(propertyName); EntityEntry copyEntry = source.PersistenceContext.GetEntry(copy); if (propertyFromCopy == null || !propertyType.IsEntityType) { log.Info("property '{0}.{1}' is null or not an entity; {1} =[{2}]", copyEntry.EntityName, propertyName, propertyFromCopy); throw; } if (!copyCache.Contains(propertyFromEntity)) { log.Info("property '{0}.{1}' from original entity is not in copyCache; {1} =[{2}]", copyEntry.EntityName, propertyName, propertyFromEntity); throw; } if (((EventCache)copyCache).IsOperatedOn(propertyFromEntity)) { log.Info(ex, "property '{0}.{1}' from original entity is in copyCache and is in the process of being merged; {1} =[{2}]", copyEntry.EntityName, propertyName, propertyFromEntity); } else { log.Info(ex, "property '{0}.{1}' from original entity is in copyCache and is not in the process of being merged; {1} =[{2}]", copyEntry.EntityName, propertyName, propertyFromEntity); } // continue...; we'll find out if it ends up not getting saved later } // cascade first, so that all unsaved objects get their // copy created before we actually copy await(base.CascadeAfterSaveAsync(source, persister, entity, copyCache, cancellationToken)).ConfigureAwait(false); await(CopyValuesAsync(persister, entity, copy, source, copyCache, ForeignKeyDirection.ForeignKeyToParent, cancellationToken)).ConfigureAwait(false); return(copy); }
private object GetIndexInParent(string property, object childEntity, IEntityPersister persister, ICollectionPersister collectionPersister, object potentialParent) { object collection = persister.GetPropertyValue(potentialParent, property, session.EntityMode); if (collection != null && NHibernateUtil.IsInitialized(collection)) { return collectionPersister.CollectionType.IndexOf(collection, childEntity); } return null; }
private bool IsFoundInParent(string property, object childEntity, IEntityPersister persister, ICollectionPersister collectionPersister, object potentialParent) { object collection = persister.GetPropertyValue(potentialParent, property, session.EntityMode); return collection != null && NHibernateUtil.IsInitialized(collection) && collectionPersister.CollectionType.Contains(collection, childEntity, session); }
/// <summary> /// Cascade an action from the parent object to all its children. /// </summary> /// <param name="session"></param> /// <param name="persister"></param> /// <param name="parent"></param> /// <param name="action"></param> /// <param name="cascadeTo"></param> /// <param name="anything"></param> public static void Cascade(ISessionImplementor session, IEntityPersister persister, object parent, CascadingAction action, CascadePoint cascadeTo, object anything) { if (persister.HasCascades) { if (log.IsDebugEnabled) { log.Debug("processing cascades for: " + persister.ClassName); } IType[] types = persister.PropertyTypes; CascadeStyle[] cascadeStyles = persister.PropertyCascadeStyles; for (int i = 0; i < types.Length; i++) { CascadeStyle style = cascadeStyles[i]; if (style.DoCascade(action)) { Cascade(session, persister.GetPropertyValue(parent, i), types[i], action, style, cascadeTo, anything); } } if (log.IsDebugEnabled) { log.Debug("done processing cascades for: " + persister.ClassName); } } }
/// <summary> /// Cascade an action from the parent entity instance to all its children. This /// form is typicaly called from within cascade actions. /// </summary> /// <param name="persister">The parent's entity persister </param> /// <param name="parent">The parent reference. </param> /// <param name="anything"> /// Typically some form of cascade-local cache /// which is specific to each CascadingAction type /// </param> public void CascadeOn(IEntityPersister persister, object parent, object anything) { if (persister.HasCascades || action.RequiresNoCascadeChecking) { log.Info("processing cascade " + action + " for: " + persister.EntityName); IType[] types = persister.PropertyTypes; CascadeStyle[] cascadeStyles = persister.PropertyCascadeStyles; EntityMode entityMode = eventSource.EntityMode; bool hasUninitializedLazyProperties = persister.HasUninitializedLazyProperties(parent, entityMode); for (int i = 0; i < types.Length; i++) { CascadeStyle style = cascadeStyles[i]; if (hasUninitializedLazyProperties && persister.PropertyLaziness[i] && !action.PerformOnLazyProperty) { //do nothing to avoid a lazy property initialization continue; } if (style.DoCascade(action)) { CascadeProperty(persister.GetPropertyValue(parent, i, entityMode), types[i], style, anything, false); } else if (action.RequiresNoCascadeChecking) { action.NoCascade(eventSource, persister.GetPropertyValue(parent, i, entityMode), parent, persister, i); } } log.Info("done processing cascade " + action + " for: " + persister.EntityName); } }