Example #1
0
        public void DeleteRows(PersistentCollection collection, object id, ISessionImplementor session)
        {
            if (!isInverse)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Deleting rows of collection: " + MessageHelper.InfoString(this, id));
                }

                try
                {
                    // delete all the deleted entries
                    ICollection entries = collection.GetDeletes(elementType);
                    if (entries.Count > 0)
                    {
                        IDbCommand st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString);
                        try
                        {
                            foreach (object entry in entries)
                            {
                                if (!hasIdentifier)
                                {
                                    WriteKey(st, id, false, session);
                                }
                                WriteRowSelect(st, entry, session);
                                session.Batcher.AddToBatch(-1);
                            }
                        }
                        // TODO: change to SqlException
                        catch (Exception e)
                        {
                            session.Batcher.AbortBatch(e);
                            throw;
                        }

                        if (log.IsDebugEnabled)
                        {
                            log.Debug("done deleting collection rows");
                        }
                    }
                    else
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("no rows to delete");
                        }
                    }
                }
                catch (HibernateException)
                {
                    // Do not call Convert on HibernateExceptions
                    throw;
                }
                catch (Exception sqle)
                {
                    throw Convert(sqle, "could not delete collection rows: " + MessageHelper.InfoString(this, id));
                }
            }
        }
Example #2
0
        public static void IdentityRemoveAll(IList list, ICollection collection, ISessionImplementor session)
        {
            IEnumerator enumer = collection.GetEnumerator();

            while (enumer.MoveNext())
            {
                PersistentCollection.IdentityRemove(list, enumer.Current, session);
            }
        }
Example #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="snapshot"></param>
        /// <returns></returns>
        public override ICollection GetOrphans(object snapshot)
        {
            IList     sn     = ( IList )snapshot;
            ArrayList result = new ArrayList(sn.Count);

            result.AddRange(sn);
            PersistentCollection.IdentityRemoveAll(result, list, Session);
            return(result);
        }
Example #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="snapshot"></param>
        /// <returns></returns>
        public override ICollection GetOrphans(object snapshot)
        {
            IDictionary sn     = ( IDictionary )snapshot;
            ArrayList   result = new ArrayList(sn.Values.Count);

            result.AddRange(sn.Values);
            PersistentCollection.IdentityRemoveAll(result, map.Values, Session);
            return(result);
        }
        public override ICollection GetOrphans(object snapshot)
        {
            /*
             * IDictionary sn = ( IDictionary ) GetSnapshot();
             * ArrayList result = new ArrayList();
             * result.AddRange( sn.Values );
             * PersistentCollection.IdentityRemoveAll( result, values, session );
             * return result;
             */

            return(PersistentCollection.GetOrphans((( IDictionary )snapshot).Values, values, Session));
        }
Example #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="snapshot"></param>
        /// <returns></returns>
        public override ICollection GetOrphans(object snapshot)
        {
            /*
             * IDictionary sn = ( IDictionary ) snapshot;
             * ArrayList result = new ArrayList( sn.Keys.Count );
             * result.AddRange( sn.Keys );
             * PersistentCollection.IdentityRemoveAll( result, internalSet, Session );
             * return result;
             */
            IDictionary sn = ( IDictionary )snapshot;

            return(PersistentCollection.GetOrphans(sn.Keys, internalSet, Session));
        }
 protected override int DoUpdateRows(object id, PersistentCollection collection, ISessionImplementor session)
 {
     try
     {
         IDbCommand  st      = null;
         ICollection entries = collection.Entries();
         try
         {
             int i     = 0;
             int count = 0;
             foreach (object entry in entries)
             {
                 if (collection.NeedsUpdating(entry, i, ElementType))
                 {
                     if (st == null)
                     {
                         st = session.Batcher.PrepareBatchCommand(SqlUpdateRowString);
                     }
                     if (!hasIdentifier)
                     {
                         WriteKey(st, id, true, session);
                     }
                     collection.WriteTo(st, this, entry, i, true);
                     session.Batcher.AddToBatch(1);
                     count++;
                 }
                 i++;
             }
             return(count);
         }
         catch (Exception e)
         {
             //TODO: change to SqlException
             // NB This calls cmd.Dispose
             session.Batcher.AbortBatch(e);
             throw;
         }
     }
     catch (HibernateException)
     {
         // Do not call Convert on HibernateExceptions
         throw;
     }
     catch (Exception sqle)
     {
         throw Convert(sqle, "could not update collection rows: " + MessageHelper.InfoString(this, id));
     }
 }
        /// <summary>
        ///
        /// </summary>
        /// <param name="snapshot"></param>
        /// <returns></returns>
        public override ICollection GetOrphans(object snapshot)
        {
            object[]  sn     = ( object[] )snapshot;
            object[]  arr    = ( object[] )array;
            ArrayList result = new ArrayList(sn.Length);

            for (int i = 0; i < sn.Length; i++)
            {
                result.Add(sn[i]);
            }
            for (int i = 0; i < sn.Length; i++)
            {
                PersistentCollection.IdentityRemove(result, arr[i], Session);
            }
            return(result);
        }
Example #9
0
        public void UpdateRows(PersistentCollection collection, object id, ISessionImplementor session)
        {
            if (!isInverse)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("Updating rows of collection: {0}#{1}", role, id));
                }

                // update all the modified entries
                int count = DoUpdateRows(id, collection, session);

                if (log.IsDebugEnabled)
                {
                    log.Debug(string.Format("done updating rows: {0} updated", count));
                }
            }
        }
		private static void DeleteOrphans( PersistentCollection pc, ISessionImplementor session )
		{
			if( pc.WasInitialized ) // can't be any orphans if it was not initialized
			{
				ICollection orphanColl = session.GetOrphans( pc );
				foreach( object obj in orphanColl )
				{
					session.Delete( obj );
				}
			}
		}
		/// <summary>
		/// Prepares this CollectionEntry for the Flush process.
		/// </summary>
		/// <param name="collection">The <see cref="PersistentCollection"/> that this CollectionEntry will be responsible for flushing.</param>
		internal void PreFlush( PersistentCollection collection )
		{
			// if the collection is initialized and it was previously persistent
			// initialize the dirty flag
			dirty = ( initialized && loadedPersister != null && IsDirty( collection ) ) ||
				( !initialized && dirty ); //only need this so collection with queued adds will be removed from second-level cache

			if( log.IsDebugEnabled && dirty && loadedPersister != null )
			{
				log.Debug( "Collection dirty: " + MessageHelper.InfoString( loadedPersister, loadedKey ) );
			}

			// reset all of these values so any previous flush status 
			// information is cleared from this CollectionEntry
			doupdate = false;
			doremove = false;
			dorecreate = false;
			reached = false;
			processed = false;
		}
		public ICollection GetOrphans( PersistentCollection coll )
		{
			CollectionEntry ce = GetCollectionEntry( coll );
			return ce.IsNew ? EmptyCollection : coll.GetOrphans( ce.Snapshot );
		}
		public void InsertRows( PersistentCollection collection, object id, ISessionImplementor session )
		{
			if( !isInverse )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( "Inserting rows of collection: " + role + "#" + id );
				}

				collection.PreInsert( this );
				int i = 0;
				int count = 0;

				try
				{
					// insert all the new entries
					ICollection entries = collection.Entries();
					try
					{
						// Moved the IDbCommand outside the loop, because ADO.NET doesn't do batch commands,
						// so it's more efficient. But it
						foreach( object entry in entries )
						{
							if( collection.NeedsInserting( entry, i, elementType ) )
							{
								IDbCommand st = null;
								if( st == null )
								{
									st = session.Batcher.PrepareBatchCommand( SqlInsertRowString );
								}
								WriteKey( st, id, false, session );
								collection.WriteTo( st, this, entry, i, false );
								session.Batcher.AddToBatch( 1 );
								collection.AfterRowInsert( this, entry, i );
								count++;
							}
							i++;
						}

						if( log.IsDebugEnabled )
						{
							log.Debug( string.Format( "done inserting rows: {0} inserted", count ) );
						}
					}
					catch( Exception e )
					{
						session.Batcher.AbortBatch( e );
						throw;
					}
				}
				catch( HibernateException )
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch( Exception sqle )
				{
					throw Convert( sqle, "could not insert collection rows: " + MessageHelper.InfoString( this, id ) );
				}

			}
		}
		protected abstract int DoUpdateRows( object key, PersistentCollection collection, ISessionImplementor session );
		private void AddUninitializedDetachedCollection( PersistentCollection collection, ICollectionPersister persister, object id )
		{
			CollectionEntry ce = new CollectionEntry( persister, id );
			collection.CollectionSnapshot = ce;
			AddCollection( collection, ce, id );
		}
		public void Recreate( PersistentCollection collection, object id, ISessionImplementor session )
		{
			if( !isInverse )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( "Inserting collection: " + MessageHelper.InfoString( this, id ) );
				}

				try
				{
					// create all the new entries
					ICollection entries = collection.Entries();
					if( entries.Count > 0 )
					{
						int i = 0;
						int count = 0;
						try
						{
							collection.PreInsert( this );

							foreach( object entry in entries )
							{
								if( collection.EntryExists( entry, i ) )
								{
									IDbCommand st = session.Batcher.PrepareBatchCommand( SqlInsertRowString );
									WriteKey( st, id, false, session );
									collection.WriteTo( st, this, entry, i, false );
									session.Batcher.AddToBatch( 1 );
									collection.AfterRowInsert( this, entry, i );
									count++;
								}
								i++;
							}
						} 
							//TODO: change to SqlException
						catch( Exception e )
						{
							session.Batcher.AbortBatch( e );
							throw;
						}
						if( log.IsDebugEnabled )
						{
							log.Debug( string.Format( "done inserting collection: {0} rows inserted", count ) );
						}
					}
					else
					{
						if( log.IsDebugEnabled )
						{
							log.Debug( "collection was empty" );
						}
					}
				}
				catch( HibernateException )
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch( Exception sqle )
				{
					throw Convert( sqle, "could not insert collection: " + MessageHelper.InfoString( this, id ) );
				}
			}
		}
		public object GetLoadedCollectionKey( PersistentCollection coll )
		{
			return GetCollectionEntry( coll ).loadedKey;
		}
Example #18
0
        public void InsertRows(PersistentCollection collection, object id, ISessionImplementor session)
        {
            if (!isInverse)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Inserting rows of collection: " + role + "#" + id);
                }

                collection.PreInsert(this);
                int i     = 0;
                int count = 0;

                try
                {
                    // insert all the new entries
                    ICollection entries = collection.Entries();
                    try
                    {
                        // Moved the IDbCommand outside the loop, because ADO.NET doesn't do batch commands,
                        // so it's more efficient. But it
                        foreach (object entry in entries)
                        {
                            if (collection.NeedsInserting(entry, i, elementType))
                            {
                                IDbCommand st = null;
                                if (st == null)
                                {
                                    st = session.Batcher.PrepareBatchCommand(SqlInsertRowString);
                                }
                                WriteKey(st, id, false, session);
                                collection.WriteTo(st, this, entry, i, false);
                                session.Batcher.AddToBatch(1);
                                collection.AfterRowInsert(this, entry, i);
                                count++;
                            }
                            i++;
                        }

                        if (log.IsDebugEnabled)
                        {
                            log.Debug(string.Format("done inserting rows: {0} inserted", count));
                        }
                    }
                    catch (Exception e)
                    {
                        session.Batcher.AbortBatch(e);
                        throw;
                    }
                }
                catch (HibernateException)
                {
                    // Do not call Convert on HibernateExceptions
                    throw;
                }
                catch (Exception sqle)
                {
                    throw Convert(sqle, "could not insert collection rows: " + MessageHelper.InfoString(this, id));
                }
            }
        }
		private CollectionEntry GetCollectionEntry( PersistentCollection coll )
		{
			return ( CollectionEntry ) collectionEntries[ coll ];
		}
Example #20
0
        public void Recreate(PersistentCollection collection, object id, ISessionImplementor session)
        {
            if (!isInverse)
            {
                if (log.IsDebugEnabled)
                {
                    log.Debug("Inserting collection: " + MessageHelper.InfoString(this, id));
                }

                try
                {
                    // create all the new entries
                    ICollection entries = collection.Entries();
                    if (entries.Count > 0)
                    {
                        int i     = 0;
                        int count = 0;
                        try
                        {
                            collection.PreInsert(this);

                            foreach (object entry in entries)
                            {
                                if (collection.EntryExists(entry, i))
                                {
                                    IDbCommand st = session.Batcher.PrepareBatchCommand(SqlInsertRowString);
                                    WriteKey(st, id, false, session);
                                    collection.WriteTo(st, this, entry, i, false);
                                    session.Batcher.AddToBatch(1);
                                    collection.AfterRowInsert(this, entry, i);
                                    count++;
                                }
                                i++;
                            }
                        }
                        //TODO: change to SqlException
                        catch (Exception e)
                        {
                            session.Batcher.AbortBatch(e);
                            throw;
                        }
                        if (log.IsDebugEnabled)
                        {
                            log.Debug(string.Format("done inserting collection: {0} rows inserted", count));
                        }
                    }
                    else
                    {
                        if (log.IsDebugEnabled)
                        {
                            log.Debug("collection was empty");
                        }
                    }
                }
                catch (HibernateException)
                {
                    // Do not call Convert on HibernateExceptions
                    throw;
                }
                catch (Exception sqle)
                {
                    throw Convert(sqle, "could not insert collection: " + MessageHelper.InfoString(this, id));
                }
            }
        }
		/// <summary>
		/// Try to initialize a Collection from the cache.
		/// </summary>
		/// <param name="id"></param>
		/// <param name="owner"></param>
		/// <param name="persister"></param>
		/// <param name="collection"></param>
		/// <returns><c>true</c> if the collection was initialized from the cache, otherwise <c>false</c>.</returns>
		private bool InitializeCollectionFromCache( object id, object owner, ICollectionPersister persister, PersistentCollection collection )
		{
			if( !persister.HasCache )
			{
				return false;
			}
			else
			{
				object cached = persister.Cache.Get( id, Timestamp );
				if( cached == null )
				{
					return false;
				}
				else
				{
					collection.InitializeFromCache( persister, cached, owner );
					GetCollectionEntry( collection ).PostInitialize( collection );
					//addInitializedCollection(collection, persister, id); h2.1 - commented out
					return true;
				}
			}
		}
		private void EvictCollection( PersistentCollection collection )
		{
			CollectionEntry ce = ( CollectionEntry ) collectionEntries[ collection ];
			collectionEntries.Remove( collection );
			if( log.IsDebugEnabled )
			{
				log.Debug( "evicting collection: " + MessageHelper.InfoString( ce.loadedPersister, ce.loadedKey ) );
			}
			if( ce.loadedPersister != null && ce.loadedKey != null )
			{
				//TODO: is this 100% correct?
				collectionsByKey.Remove( new CollectionKey( ce.loadedPersister.Role, ce.loadedKey ) );
			}
		}
		/// <summary>
		/// called by a collection that wants to initialize itself
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="writing"></param>
		public void InitializeCollection( PersistentCollection collection, bool writing )
		{
			CollectionEntry ce = GetCollectionEntry( collection );

			if( ce == null )
			{
				throw new HibernateException( "collection was evicted" );
			}

			if( !ce.initialized )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( "initializing collection " + MessageHelper.InfoString( ce.loadedPersister, ce.loadedKey ) );
				}

				log.Debug( "checking second-level cache" );

				bool foundInCache = InitializeCollectionFromCache( ce.loadedKey, GetCollectionOwner( ce ), ce.loadedPersister, collection );

				if( foundInCache )
				{
					log.Debug( "collection initialized from cache" );
				}
				else
				{
					log.Debug( "collection not cached" );
					ce.loadedPersister.Initialize( ce.loadedKey, this );
					log.Debug( "collection initialized" );
				}
			}
		}
		private CollectionEntry AddCollection( PersistentCollection collection )
		{
			CollectionEntry ce = new CollectionEntry();
			collectionEntries[ collection ] = ce;
			collection.CollectionSnapshot = ce;
			return ce;
		}
		/// <summary>
		/// Updates the CollectionEntry to reflect that it is has been successfully flushed to the database.
		/// </summary>
		/// <param name="collection">The <see cref="PersistentCollection"/> that was flushed.</param>
		/// <remarks>
		/// Called after a <em>successful</em> flush.
		/// </remarks>
		internal bool PostFlush( PersistentCollection collection )
		{
			if( ignore )
			{
				ignore = false;
			}
			else
			{
				// the CollectionEntry should be processed if we are in the PostFlush()
				if( !processed )
				{
					throw new AssertionFailure( "collection was not processed by Flush()" );
				}

				// now that the flush has gone through move everything that is the current
				// over to the loaded fields and set dirty to false since the db & collection
				// are in synch.
				loadedKey = currentKey;
				SetLoadedPersister( currentPersister );
				dirty = false;

				// collection needs to know its' representation in memory and with
				// the db is now in synch - esp important for collections like a bag
				// that can add without initializing the collection.
				collection.PostFlush();

				// if it was initialized or any of the scheduled actions were performed then
				// need to resnapshot the contents of the collection.
				if( initialized && ( doremove || dorecreate || doupdate ) )
				{
					InitSnapshot( collection, loadedPersister );
				}
			}

			return loadedPersister == null;
		}
		/// <summary>
		/// add a collection we just pulled out of the cache (does not need initializing)
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="persister"></param>
		/// <param name="id"></param>
		public CollectionEntry AddInitializedCollection( PersistentCollection collection, ICollectionPersister persister, object id )
		{
			CollectionEntry ce = new CollectionEntry( persister, id, flushing );
			ce.PostInitialize( collection );
			collection.CollectionSnapshot = ce;
			AddCollection( collection, ce, id );

			return ce;
		}
		public object GetSnapshot( PersistentCollection coll )
		{
			return GetCollectionEntry( coll ).snapshot;
		}
Example #28
0
        protected override int DoUpdateRows(object id, PersistentCollection collection, ISessionImplementor session)
        {
            // we finish all the "removes" first to take care of possible unique
            // constraints and so that we can take better advantage of batching

            IDbCommand  st;
            ICollection entries;
            int         i;
            int         count;

            try
            {
                // update removed rows fks to null
                count = 0;
                try
                {
                    st      = null;
                    i       = 0;
                    entries = collection.Entries();
                    foreach (object entry in entries)
                    {
                        if (collection.NeedsUpdating(entry, i, ElementType))                            // will still be issued when it used to be null
                        {
                            if (st == null)
                            {
                                st = session.Batcher.PrepareBatchCommand(SqlDeleteRowString);
                            }
                            WriteKey(st, id, false, session);
                            WriteIndex(st, collection.GetIndex(entry, i), false, session);
                            session.Batcher.AddToBatch(-1);
                            count++;
                        }
                        i++;
                    }
                }
                catch (Exception e)
                {
                    session.Batcher.AbortBatch(e);
                    throw;
                }

                // now update all changed or added rows fks
                count = 0;
                try
                {
                    st      = null;
                    i       = 0;
                    entries = collection.Entries();
                    foreach (object entry in entries)
                    {
                        if (collection.NeedsUpdating(entry, i, ElementType))                            // will still be issued when it used to be null
                        {
                            if (st == null)
                            {
                                st = session.Batcher.PrepareBatchCommand(SqlInsertRowString);
                            }
                            WriteKey(st, id, false, session);
                            collection.WriteTo(st, this, entry, i, false);
                            session.Batcher.AddToBatch(1);
                            count++;
                        }
                        i++;
                    }
                }
                catch (Exception e)
                {
                    //TODO: change to SqlException
                    session.Batcher.AbortBatch(e);
                    throw;
                }
                return(count);
            }
            catch (HibernateException)
            {
                // Do not call Convert on HibernateExceptions
                throw;
            }
            catch (Exception sqle)
            {
                throw Convert(sqle, "could not update collection rows: " + MessageHelper.InfoString(this, id));
            }
        }
		public void DeleteRows(PersistentCollection collection, object key, ISessionImplementor session)
		{
			// TODO:  Add CollectionPersisterStub.DeleteRows implementation
		}
		public void DeleteRows( PersistentCollection collection, object id, ISessionImplementor session )
		{
			if( !isInverse )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( "Deleting rows of collection: " + MessageHelper.InfoString( this, id ) );
				}

				try
				{
					// delete all the deleted entries
					ICollection entries = collection.GetDeletes( elementType );
					if( entries.Count > 0 )
					{
						IDbCommand st = session.Batcher.PrepareBatchCommand( SqlDeleteRowString );
						try
						{
							foreach( object entry in entries )
							{
								if( !hasIdentifier )
								{
									WriteKey( st, id, false, session );
								}
								WriteRowSelect( st, entry, session );
								session.Batcher.AddToBatch( -1 );
							}
						} 
							// TODO: change to SqlException
						catch( Exception e )
						{
							session.Batcher.AbortBatch( e );
							throw;
						}

						if( log.IsDebugEnabled )
						{
							log.Debug( "done deleting collection rows" );
						}
					}
					else
					{
						if( log.IsDebugEnabled )
						{
							log.Debug( "no rows to delete" );
						}
					}
				}
				catch( HibernateException )
				{
					// Do not call Convert on HibernateExceptions
					throw;
				}
				catch( Exception sqle )
				{
					throw Convert( sqle, "could not delete collection rows: " + MessageHelper.InfoString( this, id ) );
				}
			}
		}
		/// <summary>
		/// Add an (initialized) collection that was created by another session and passed
		/// into update() (i.e. one with a snapshot and existing state on the database)
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="cs"></param>
		private void AddInitializedDetachedCollection( PersistentCollection collection, ICollectionSnapshot cs )
		{
			if( cs.WasDereferenced )
			{
				AddCollection( collection );
			}
			else
			{
				CollectionEntry ce = new CollectionEntry( cs, factory );
				collection.CollectionSnapshot = ce;
				AddCollection( collection, ce, cs.Key );
			}
		}
		public void UpdateRows( PersistentCollection collection, object id, ISessionImplementor session )
		{
			if( !isInverse )
			{
				if( log.IsDebugEnabled )
				{
					log.Debug( string.Format( "Updating rows of collection: {0}#{1}", role, id ) );
				}

				// update all the modified entries
				int count = DoUpdateRows( id, collection, session );

				if( log.IsDebugEnabled )
				{
					log.Debug( string.Format( "done updating rows: {0} updated", count ) );
				}
			}
		}
		protected override int DoUpdateRows( object id, PersistentCollection collection, ISessionImplementor session )
		{
			// we finish all the "removes" first to take care of possible unique 
			// constraints and so that we can take better advantage of batching

			IDbCommand st;
			ICollection entries;
			int i;
			int count;

			try
			{
				// update removed rows fks to null
				count = 0;
				try
				{
					st = null;
					i = 0;
					entries = collection.Entries();
					foreach( object entry in entries )
					{
						if( collection.NeedsUpdating( entry, i, ElementType ) ) // will still be issued when it used to be null
						{
							if( st == null )
							{
								st = session.Batcher.PrepareBatchCommand( SqlDeleteRowString );
							}
							WriteKey( st, id, false, session );
							WriteIndex( st, collection.GetIndex( entry, i ), false, session );
							session.Batcher.AddToBatch( -1 );
							count++;
						}
						i++;
					}
				}
				catch( Exception e )
				{
					session.Batcher.AbortBatch( e );
					throw;
				}

				// now update all changed or added rows fks
				count = 0;
				try
				{
					st = null;
					i = 0;
					entries = collection.Entries();
					foreach( object entry in entries )
					{
						if( collection.NeedsUpdating( entry, i, ElementType ) ) // will still be issued when it used to be null
						{
							if( st == null )
							{
								st = session.Batcher.PrepareBatchCommand( SqlInsertRowString );
							}
							WriteKey( st, id, false, session );
							collection.WriteTo( st, this, entry, i, false );
							session.Batcher.AddToBatch( 1 );
							count++;
						}
						i++;
					}
				}
				catch( Exception e )
				{
					//TODO: change to SqlException
					session.Batcher.AbortBatch( e );
					throw;
				}
				return count;
			}
			catch( HibernateException )
			{
				// Do not call Convert on HibernateExceptions
				throw;
			}
			catch( Exception sqle )
			{
				throw Convert( sqle, "could not update collection rows: " + MessageHelper.InfoString( this, id ) );
			}
		}
		/// <summary>
		/// Checks to see if the <see cref="PersistentCollection"/> has had any changes to the 
		/// collections contents or if any of the elements in the collection have been modified.
		/// </summary>
		/// <param name="coll"></param>
		/// <returns><c>true</c> if the <see cref="PersistentCollection"/> is dirty.</returns>
		/// <remarks>
		/// default behavior; will be overridden in deep lazy collections
		/// </remarks>
		private bool IsDirty( PersistentCollection coll )
		{
			// if this has already been marked as dirty or the collection can not 
			// be directly accessed (ie- we can guarantee that the NHibernate collection
			// wrappers are used) and the elements in the collection are not mutable 
			// then return the dirty flag.
			if( dirty || (
				!coll.IsDirectlyAccessible && !loadedPersister.ElementType.IsMutable
				) )
			{
				return dirty;
			}
			else
			{
				// need to have the coll determine if it is the same as the snapshot
				// that was last taken.
				return !coll.EqualsSnapshot( loadedPersister.ElementType );
			}
		}
		/// <summary>
		/// Add a new collection (i.e. a newly created one, just instantiated by
		/// the application, with no database state or snapshot)
		/// </summary>
		/// <param name="collection"></param>
		/// <param name="persister"></param>
		internal void AddNewCollection( PersistentCollection collection, ICollectionPersister persister )
		{
			CollectionEntry ce = AddCollection( collection );
			if( persister.HasOrphanDelete )
			{
				ce.InitSnapshot( collection, persister );
			}

		}
		/// <summary>
		/// Updates the CollectionEntry to reflect that the <see cref="PersistentCollection"/>
		/// has been initialized.
		/// </summary>
		/// <param name="collection">The initialized <see cref="PersistentCollection"/> that this Entry is for.</param>
		internal void PostInitialize( PersistentCollection collection )
		{
			initialized = true;
			snapshot = collection.GetSnapshot( loadedPersister );
		}
		public bool IsInverseCollection( PersistentCollection collection )
		{
			CollectionEntry ce = GetCollectionEntry( collection );
			return ce != null && ce.loadedPersister.IsInverse;
		}
		internal void InitSnapshot( PersistentCollection collection, ICollectionPersister persister )
		{
			snapshot = collection.GetSnapshot( persister );
		}
Example #39
0
 protected abstract int DoUpdateRows(object key, PersistentCollection collection, ISessionImplementor session);
		/// <summary>
		/// Initializes a new instance of <see cref="ScheduledCollectionUpdate"/>.
		/// </summary>
		/// <param name="collection">The <see cref="PersistentCollection"/> to update.</param>
		/// <param name="persister">The <see cref="ICollectionPersister"/> that is responsible for the persisting the Collection.</param>
		/// <param name="id">The identifier of the Collection owner.</param>
		/// <param name="emptySnapshot">Indicates if the Collection was empty when it was loaded.</param>
		/// <param name="session">The <see cref="ISessionImplementor"/> that the Action is occuring in.</param>
		public ScheduledCollectionUpdate( PersistentCollection collection, ICollectionPersister persister, object id, bool emptySnapshot, ISessionImplementor session )
			: base( persister, id, session )
		{
			_collection = collection;
			_emptySnapshot = emptySnapshot;
		}
		protected override int DoUpdateRows( object id, PersistentCollection collection, ISessionImplementor session )
		{
			try
			{
				IDbCommand st = null;
				ICollection entries = collection.Entries();
				try
				{
					int i = 0;
					int count = 0;
					foreach( object entry in entries )
					{
						if( collection.NeedsUpdating( entry, i, ElementType ) )
						{
							if( st == null )
							{
								st = session.Batcher.PrepareBatchCommand( SqlUpdateRowString );
							}
							if( !hasIdentifier )
							{
								WriteKey( st, id, true, session );
							}
							collection.WriteTo( st, this, entry, i, true );
							session.Batcher.AddToBatch( 1 );
							count++;
						}
						i++;
					}
					return count;
				}
				catch( Exception e )
				{
					//TODO: change to SqlException
					// NB This calls cmd.Dispose
					session.Batcher.AbortBatch( e );
					throw;
				}
			}
			catch( HibernateException )
			{
				// Do not call Convert on HibernateExceptions
				throw;
			}
			catch( Exception sqle )
			{
				throw Convert( sqle, "could not update collection rows: " + MessageHelper.InfoString( this, id ) );
			}
		}
		public void Recreate(PersistentCollection collection, object key, ISessionImplementor session)
		{
			// TODO:  Add CollectionPersisterStub.Recreate implementation
		}
		/// <summary>
		/// Initializes a new instance of <see cref="ScheduledCollectionRecreate"/>.
		/// </summary>
		/// <param name="collection">The <see cref="PersistentCollection"/> to recreate.</param>
		/// <param name="persister">The <see cref="ICollectionPersister"/> that is responsible for the persisting the Collection.</param>
		/// <param name="id">The identifier of the Collection owner.</param>
		/// <param name="session">The <see cref="ISessionImplementor"/> that the Action is occuring in.</param>
		public ScheduledCollectionRecreate( PersistentCollection collection, ICollectionPersister persister, object id, ISessionImplementor session )
			: base( persister, id, session )
		{
			_collection = collection;
		}