public object GetObjectToSerialize(object obj, Type targetType)
 {
     // Serialize proxies as the base type
     if (obj is INHibernateProxy)
     {
         // Getting the implementation of the proxy forces an initialization of the proxied object (if not yet initialized)
         try
         {
             var newobject = ((INHibernateProxy)obj).HibernateLazyInitializer.GetImplementation();
             obj = newobject;
         }
         catch (Exception)
         {
             // Type test = NHibernateProxyHelper.GetClassWithoutInitializingProxy(obj);
             obj = null;
         }
     }
     // Serialize persistent collections as the collection interface type
     if (obj is IPersistentCollection)
     {
         IPersistentCollection persistentCollection = (IPersistentCollection)obj;
         persistentCollection.ForceInitialization();
         //obj = persistentCollection.Entries(); // This returns the "wrapped" collection
         obj = persistentCollection.Entries(null);     // This returns the "wrapped" collection
     }
     return(obj);
 }
Beispiel #2
0
        private static void PrepareCollectionForUpdate(IPersistentCollection collection, CollectionEntry entry, ISessionFactoryImplementor factory)
        {
            //1. record the collection role that this collection is referenced by
            //2. decide if the collection needs deleting/creating/updating (but don't actually schedule the action yet)

            if (entry.IsProcessed)
            {
                throw new AssertionFailure("collection was processed twice by flush()");
            }

            entry.IsProcessed = true;

            ICollectionPersister loadedPersister  = entry.LoadedPersister;
            ICollectionPersister currentPersister = entry.CurrentPersister;

            if (loadedPersister != null || currentPersister != null)
            {
                // it is or was referenced _somewhere_
                bool ownerChanged = loadedPersister != currentPersister ||
                                    !currentPersister.KeyType.IsEqual(entry.LoadedKey, entry.CurrentKey, factory);

                if (ownerChanged)
                {
                    // do a check
                    bool orphanDeleteAndRoleChanged = loadedPersister != null &&
                                                      currentPersister != null && loadedPersister.HasOrphanDelete;

                    if (orphanDeleteAndRoleChanged)
                    {
                        throw new HibernateException("Don't change the reference to a collection with cascade=\"all-delete-orphan\": " + loadedPersister.Role);
                    }

                    // do the work
                    if (currentPersister != null)
                    {
                        entry.IsDorecreate = true;                         // we will need to create new entries
                    }

                    if (loadedPersister != null)
                    {
                        entry.IsDoremove = true;                         // we will need to remove ye olde entries
                        if (entry.IsDorecreate)
                        {
                            log.Debug("Forcing collection initialization");
                            collection.ForceInitialization();                             // force initialize!
                        }
                    }
                }
                else if (collection.IsDirty)
                {
                    // else if it's elements changed
                    entry.IsDoupdate = true;
                }
            }
        }
Beispiel #3
0
		 //1. record the collection role that this collection is referenced by
		 //2. decide if the collection needs deleting/creating/updating (but don't actually schedule the action yet)
		private static void PrepareCollectionForUpdate(IPersistentCollection coll, CollectionEntry entry)
		{
			if (entry.IsProcessed)
				throw new AssertionFailure("collection was processed twice by flush()");

			entry.IsProcessed = true;

			ICollectionPersister loadedPersister = entry.LoadedPersister;
			ICollectionPersister currentPersister = entry.CurrentPersister;
			if (loadedPersister != null || currentPersister != null)
			{
				// it is or was referenced _somewhere_
				bool ownerChanged = loadedPersister != currentPersister ||
					!currentPersister.KeyType.IsEqual(entry.LoadedKey, entry.CurrentKey, EntityMode.Poco);

				if (ownerChanged)
				{
					// do a check
					bool orphanDeleteAndRoleChanged = loadedPersister != null && 
						currentPersister != null && loadedPersister.HasOrphanDelete;

					if (orphanDeleteAndRoleChanged)
					{
						throw new HibernateException("Don't change the reference to a collection with cascade=\"all-delete-orphan\": " + loadedPersister.Role);
					}

					// do the work
					if (currentPersister != null)
					{
						entry.IsDorecreate = true; // we will need to create new entries
					}

					if (loadedPersister != null)
					{
						entry.IsDoremove = true; // we will need to remove ye olde entries
						if (entry.IsDorecreate)
						{
							log.Debug("Forcing collection initialization");
							coll.ForceInitialization(); // force initialize!
						}
					}
				}
				else if (coll.IsDirty)
				{
					// else if it's elements changed
					entry.IsDoupdate = true;
				}
			}
		}