示例#1
0
        /// <summary>
        /// Given that there is no pre-existing proxy.
        /// Check if the entity is already loaded. If it is, return the entity,
        /// otherwise create and return a proxy.
        /// </summary>
        private object CreateProxyIfNecessary(LoadEvent @event, IEntityPersister persister, EntityKey keyToLoad, LoadType options, IPersistenceContext persistenceContext)
        {
            object existing = persistenceContext.GetEntity(keyToLoad);

            if (existing != null)
            {
                // return existing object or initialized proxy (unless deleted)
                log.Debug("entity found in session cache");
                if (options.IsCheckDeleted)
                {
                    EntityEntry entry  = persistenceContext.GetEntry(existing);
                    Status      status = entry.Status;
                    if (status == Status.Deleted || status == Status.Gone)
                    {
                        return(null);
                    }
                }
                return(existing);
            }
            else
            {
                log.Debug("creating new proxy for entity");
                // return new uninitialized proxy
                object proxy = persister.CreateProxy(@event.EntityId, @event.Session);
                persistenceContext.BatchFetchQueue.AddBatchLoadableEntityKey(keyToLoad);
                persistenceContext.AddProxy(keyToLoad, (INHibernateProxy)proxy);
                ((INHibernateProxy)proxy)
                .HibernateLazyInitializer
                .ReadOnly = @event.Session.DefaultReadOnly || !persister.IsMutable;
                return(proxy);
            }
        }
示例#2
0
 public override object InternalLoad(string entityName, object id, bool eager, bool isNullable)
 {
     using (BeginProcess())
     {
         IEntityPersister persister = Factory.GetEntityPersister(entityName);
         object           loaded    = temporaryPersistenceContext.GetEntity(GenerateEntityKey(id, persister));
         if (loaded != null)
         {
             return(loaded);
         }
         if (!eager && persister.HasProxy)
         {
             return(persister.CreateProxy(id, this));
         }
         //TODO: if not loaded, throw an exception
         return(Get(entityName, id));
     }
 }
 public override object InternalLoad(string entityName, object id, bool eager, bool isNullable)
 {
     using (new SessionIdLoggingContext(SessionId))
     {
         CheckAndUpdateSessionStatus();
         IEntityPersister persister = Factory.GetEntityPersister(entityName);
         object           loaded    = temporaryPersistenceContext.GetEntity(GenerateEntityKey(id, persister, EntityMode.Poco));
         if (loaded != null)
         {
             return(loaded);
         }
         if (!eager && persister.HasProxy)
         {
             return(persister.CreateProxy(id, this));
         }
         //TODO: if not loaded, throw an exception
         return(Get(entityName, id));
     }
 }
示例#4
0
 public override async Task <object> InternalLoadAsync(string entityName, object id, bool eager, bool isNullable, CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     using (BeginProcess())
     {
         IEntityPersister persister = Factory.GetEntityPersister(entityName);
         object           loaded    = temporaryPersistenceContext.GetEntity(GenerateEntityKey(id, persister));
         if (loaded != null)
         {
             return(loaded);
         }
         if (!eager && persister.HasProxy)
         {
             return(persister.CreateProxy(id, this));
         }
         //TODO: if not loaded, throw an exception
         return(await(GetAsync(entityName, id, cancellationToken)).ConfigureAwait(false));
     }
 }
		/// <summary> 
		/// If the existing proxy is insufficiently "narrow" (derived), instantiate a new proxy
		/// and overwrite the registration of the old one. This breaks == and occurs only for
		/// "class" proxies rather than "interface" proxies. Also init the proxy to point to
		/// the given target implementation if necessary. 
		/// </summary>
		/// <param name="proxy">The proxy instance to be narrowed. </param>
		/// <param name="persister">The persister for the proxied entity. </param>
		/// <param name="key">The internal cache key for the proxied entity. </param>
		/// <param name="obj">(optional) the actual proxied entity instance. </param>
		/// <returns> An appropriately narrowed instance. </returns>
		public object NarrowProxy(INHibernateProxy proxy, IEntityPersister persister, EntityKey key, object obj)
		{
			bool alreadyNarrow = persister.GetConcreteProxyClass(session.EntityMode).IsAssignableFrom(proxy.GetType());

			if (!alreadyNarrow)
			{
				if (ProxyWarnLog.IsWarnEnabled)
				{
					ProxyWarnLog.Warn("Narrowing proxy to " + persister.GetConcreteProxyClass(session.EntityMode) + " - this operation breaks ==");
				}

				if (obj != null)
				{
					proxiesByKey.Remove(key);
					return obj; //return the proxied object
				}
				else
				{
					proxy = (INHibernateProxy)persister.CreateProxy(key.Identifier, session);
					proxiesByKey[key] = proxy; //overwrite old proxy
					return proxy;
				}
			}
			else
			{
				if (obj != null)
				{
					proxy.HibernateLazyInitializer.SetImplementation(obj);
				}
				return proxy;
			}
		}
示例#6
0
		/// <summary>
		/// Given that there is no pre-existing proxy.
		/// Check if the entity is already loaded. If it is, return the entity,
		/// otherwise create and return a proxy.
		/// </summary>
		private object CreateProxyIfNecessary(LoadEvent @event, IEntityPersister persister, EntityKey keyToLoad, LoadType options, IPersistenceContext persistenceContext)
		{
			object existing = persistenceContext.GetEntity(keyToLoad);
			if (existing != null)
			{
				// return existing object or initialized proxy (unless deleted)
				log.Debug("entity found in session cache");
				if (options.IsCheckDeleted)
				{
					EntityEntry entry = persistenceContext.GetEntry(existing);
					Status status = entry.Status;
					if (status == Status.Deleted || status == Status.Gone)
					{
						return null;
					}
				}
				return existing;
			}
			else
			{
				log.Debug("creating new proxy for entity");
				// return new uninitialized proxy
				object proxy = persister.CreateProxy(@event.EntityId, @event.Session);
				persistenceContext.BatchFetchQueue.AddBatchLoadableEntityKey(keyToLoad);
				persistenceContext.AddProxy(keyToLoad, (INHibernateProxy)proxy);
				((INHibernateProxy)proxy)
					.HibernateLazyInitializer
					.ReadOnly = @event.Session.DefaultReadOnly || !persister.IsMutable;
				return proxy;
			}
		}