from NHibernate documentation: 1. all entity insertions, in the same order the corresponding objects were saved using ISession.Save() 2. all entity updates 3. all collection deletions 4. all collection element deletions, updates and insertions 5. all collection insertions 6. all entity deletions, in the same order the corresponding objects were deleted using ISession.Delete()
It looks like the problem is that the item being updated is caught at step 2, as an entity is being updated ( instead of step 4). My question then would be what is a collection element? I was assuming that if I delete an entity from a collection and then update items in that collection that the deletes will occur first. On Apr 2, 8:27 am, Darren <[email protected]> wrote: > Looking back at the example, it might be a bit misleading on what I'm > trying to achieve. In the example the solution would be just delete > the book thats not needed anymore and don't worry about the update. > > The specific problem I'm having is that I have an ordered list of > items that need to be processed (almost like a print queue). The items > that have yet to be processed can be modified or removed. When an item > is removed the reference number (consecutive from 1 to n, and has the > unique constraint) need to remain consecutive hence the delete then > update. > > On Mar 25, 8:43 am, Darren <[email protected]> wrote: > > > Here's a test case, that fails on unique key constraint ie its doing > > the update before the delete. > > > I must have something wrong somewhere, but not sure where? > > > Mapping.hbm.xml: > > <?xml version="1.0" encoding="utf-8" ?> > > > <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" > > assembly="NHibernate.Test" > > namespace="NHibernate.Test.NHSpecificTest.DarrenC"> > > > <class name="Shelf" table="Shelves"> > > <id name="Id" column="Id"> > > <generator class="hilo" /> > > </id> > > > <property name="Location" column="Location" /> > > > <bag name="Books" inverse="true" cascade="all-delete-orphan"> > > <key column="ShelfId" /> > > <one-to-many class="Book" /> > > </bag> > > </class> > > > <class name="Book" table="Books"> > > <id name="Id" column="Id"> > > <generator class="hilo" /> > > </id> > > > <property name="Title" column="Title" unique="true"/> > > > <many-to-one name="Shelf" column="ShelfId" class="Shelf" /> > > > </class> > > > </hibernate-mapping> > > > Fixture: > > public class Shelf > > { > > private IList<Book> _Books; > > > /// <summary> > > /// Initializes a new instance of the Shelf class. > > /// </summary> > > public Shelf() > > { > > _Books = new List<Book>(); > > } > > > public virtual int Id { get; set; } > > public virtual string Location { get; set; } > > > public virtual IList<Book> Books > > { > > get { return _Books; } > > set { _Books = value; } > > } > > } > > > public class Book > > { > > public virtual int Id { get; set; } > > public virtual string Title { get; set; } > > public virtual Shelf Shelf { get; set; } > > } > > > [TestFixture] > > public class Fixture : BugTestCase > > { > > protected override string MappingsAssembly > > { > > get { return "NHibernate.Test"; } > > } > > > protected override IList Mappings > > { > > get { return new string[] > > { "NHSpecificTest.DarrenC.Mappings.hbm.xml" }; } > > } > > > [Test] > > public void Test() > > { > > Shelf shelf = new Shelf { Location = "Middle" }; > > > shelf.Books.Add( new Book { Title = "NHibernate In > > Action", Shelf = shelf } ); > > shelf.Books.Add( new Book { Title = "NHibernate In Action > > v2", Shelf = shelf } ); > > > using ( ISession s = OpenSession() ) > > using ( ITransaction t = s.BeginTransaction() ) > > { > > s.SaveOrUpdate( shelf ); > > t.Commit(); > > s.Flush(); > > } > > > using ( ISession s = OpenSession() ) > > using ( ITransaction t = s.BeginTransaction() ) > > { > > Shelf loadedShelf = s.Get<Shelf>( shelf.Id ); > > Assert.AreEqual( 2, loadedShelf.Books.Count ); > > > loadedShelf.Books.RemoveAt( 0 ); > > > Assert.AreEqual( "NHibernate In Action v2", > > loadedShelf.Books[0].Title ); > > // rename existing book to be the same as the removed > > book > > loadedShelf.Books[0].Title = "NHibernate In Action"; > > s.Update( loadedShelf ); > > t.Commit(); > > > Assert.AreEqual( 1, loadedShelf.Books.Count ); > > > s.Refresh( loadedShelf ); > > > Assert.AreEqual( 1, loadedShelf.Books.Count ); > > } > > } > > > protected override void OnTearDown() > > { > > ISession s = OpenSession(); > > ITransaction t = s.BeginTransaction(); > > s.Delete( "from Book" ); > > s.Delete( "from Shelf" ); > > t.Commit(); > > s.Close(); > > base.OnTearDown(); > > } > > } > > > On Mar 18, 10:17 am, Darren <[email protected]> wrote: > > > > I'll attempt to put together a test case. > > > > On Mar 16, 3:46 pm, Darren <[email protected]> wrote: > > > > > The child entity that is removed is not re-parented. The remaining > > > > child entity stays with its parent in gets the key of item that was > > > > removed. > > > > > On Mar 13, 12:04 am, Fabio Maulo <[email protected]> wrote: > > > > > > Are you re-parenting the child ? > > > > > > 2009/3/11 Darren <[email protected]> > > > > > > > In NHibernate documentation (section 9.2 - > > > > > > >http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html_si... > > > > > > ) > > > > > > I'm led to believe that when modifying collection elements > > > > > > NHibernate > > > > > > will perform deletes, updates then inserts. > > > > > > > I have the following problem: > > > > > > > I have an entity with a collection of items (the collection has > > > > > > cascade=all-delete-orphan) and the items have a unique key. I'm > > > > > > trying > > > > > > to delete an element from the collection (by doing a remove from the > > > > > > list) and then changing the key of an existing element to the one > > > > > > that > > > > > > was removed from the list. The problem I'm experiencing is that the > > > > > > update is occurring before the delete when I commit the changes to > > > > > > the > > > > > > entity and consequently violating the unique key. > > > > > > > The code looks something like this: > > > > > > > item = entity.collection[0]; > > > > > > item2 = entity.collection[1]; > > > > > > entity.collection.Remove(item); > > > > > > item2.name = item.name; > > > > > > session.SaveOrUpdate(entity); > > > > > > session.Commit; > > > > > > > Do you think this is a bug or am I doing something wrong? > > > > > > > Darren > > > > > > -- > > > > > Fabio Maulo --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
