public void CanRetryDeleteUsingNewSession()
        {
            //setup - trigger a failure when deleting...

            Guid customerOneId = DbFixture.InsertCustomer();

            var c = Nh.CurrentSession.Get <Customer>(customerOneId);

            Nh.CurrentSession.Delete(c);

            //cause transaction to fail
            Nh.CurrentSession.Save(Om.CreateInvalidCustomer());

            CommitSessionExpectingException();

            //run test...

            using (ISession newSession = Nh.CreateSession())
            {
                newSession.Delete(c);
                newSession.Flush();
            }

            Assert.That(DbFixture.CustomerExists(c), Is.False);
        }
        public void UpdateWillNotIncrementVersionProperty()
        {
            Guid id = DbFixture.InsertCustomer();
            var  c  = Nh.CurrentSession.Get <Customer>(id);

            int versionPropertyBeforeUpdate = c.ConcurrencyId;

            Nh.CurrentSession.Update(c);

            Assert.That(c.ConcurrencyId, Is.EqualTo(versionPropertyBeforeUpdate), "version property not incremented");
        }
        public void CanAttachAssociatedObjectToDifferentSessions()
        {
            //note: although this is possible would highly recommend against doing this

            //given
            Customer customer = Om.CreateCustomer();
            Order    order1   = Om.CreateOrderWith(customer);
            Order    order2   = Om.CreateOrderWith(customer);

            Nh.CommitInSameTempSession(session => {
                DbFixture.Insert(customer);
                DbFixture.Insert(order1);
                DbFixture.Insert(order2);
            });

            var order1FromDb = Nh.GetFromDb <Order>(order1.Id);
            var order2FromDb = Nh.GetFromDb <Order>(order2.Id);

            NHibernateUtil.Initialize(order1FromDb.Customer);
            Assert.That(order1FromDb.Customer, Is.SameAs(order2FromDb.Customer), "clarifying assumptions");

            //evict all our object we have just loaded
            Nh.CurrentSession.Clear();


            //run test...

            //attach first order to first session
            ISession firstSession = Nh.CreateSession();

            firstSession.Lock(order1FromDb, LockMode.None);
            Assert.That(firstSession.ExistsInSession <Order>(order1FromDb.Id), Is.True, "order not attached");
            Assert.That(firstSession.ExistsInSession <Customer>(order1FromDb.Customer.Id),
                        Is.False,
                        "customer not expected to attached");

            //attach customer to second session
            ISession secondSession = Nh.CreateSession();

            secondSession.Lock(order1FromDb.Customer, LockMode.None);
            Assert.That(secondSession.ExistsInSession <Customer>(order1FromDb.Customer.Id),
                        Is.True,
                        "customer not attached");

            //attach second order to third session
            ISession thirdSession = Nh.CreateSession();

            thirdSession.Lock(order2FromDb, LockMode.None);
            Assert.That(thirdSession.ExistsInSession <Order>(order2FromDb.Id), Is.True, "order not attached");
            Assert.That(secondSession.ExistsInSession <Customer>(order1FromDb.Customer.Id),
                        Is.True,
                        "customer no longer attached to first session");
        }
        public void LazyLoadWillFailOnceObjectDetatched()
        {
            //given
            Guid id = DbFixture.Insert(Om.CreateCustomerWithOneRep());

            //when
            var detatchedInstance = Nh.CurrentSession.Get <Customer>(id);

            Nh.CurrentSession.Evict(detatchedInstance);

            //then
            Assert.Throws <LazyInitializationException>(() => detatchedInstance.CustomerRepresentatives.At(0));
        }
        public void CanTestWhenAssociationIsLazyLoaded()
        {
            //given
            Customer customer = Om.CreateCustomerWithOneRep();

            DbFixture.Insert(customer);

            //when
            var loadedCustomer = Nh.GetFromDb <Customer>(customer.Id);

            //then
            NhAssert.IsLazyLoaded(loadedCustomer.CustomerRepresentatives);
        }
        public void ExceptionFlushingAnExistingObjectWillStopIncrementOfVersion()
        {
            Guid id = DbFixture.InsertCustomer();
            var  c  = Nh.CurrentSession.Get <Customer>(id);

            c.ShortCode = "CCCCCCCCC"; //update will fail when saved to the db

            int versionPropertyBeforeFlush = c.ConcurrencyId;

            CommitSessionExpectingException();

            Assert.That(c.ConcurrencyId,
                        Is.EqualTo(versionPropertyBeforeFlush),
                        "ConcurrencyId has NOT been incremented");
        }
        public void FlushWillIncrementVersionPropertyOnExistingObject()
        {
            Guid id = DbFixture.InsertCustomer();
            var  c  = Nh.CurrentSession.Get <Customer>(id);

            c.Name = "George, Katie";

            int versionPropertyBeforeFlush = c.ConcurrencyId;

            Nh.CurrentSession.Flush();

            Assert.That(c.ConcurrencyId,
                        Is.EqualTo(versionPropertyBeforeFlush + 1),
                        "version property incremented");
        }
        public void CanGetObjectFromFirstLevelCacheByIdentifier()
        {
            Guid customerId = DbFixture.InsertCustomer();

            EntityKey key = EntityKeyFor <Customer>(customerId);

            Nh.CurrentSession.Get <Customer>(customerId);
            Assert.That(Nh.CurrentSession.GetSessionImplementation().GetEntityUsingInterceptor(key),
                        Is.Not.Null,
                        "entity found");

            Nh.CurrentSession.Clear();
            Assert.That(Nh.CurrentSession.GetSessionImplementation().GetEntityUsingInterceptor(key),
                        Is.Null,
                        "entity not found");
        }
        public void ExceptionFlushingAnExistingObjectWillNotStopVsIncrementOfChildObject()
        {
            Guid id = DbFixture.InsertCustomerWithAddress();
            var  c  = Nh.CurrentSession.Get <Customer>(id);

            Address a = c.AddressAt(0);

            a.Line1 = "Another street";

            c.ShortCode = "CCCCCCCCC"; //update will fail when saved to the db

            int versionPropertyBeforeFlush = a.ConcurrencyId;

            CommitSessionExpectingException();

            Assert.That(a.ConcurrencyId,
                        Is.EqualTo(versionPropertyBeforeFlush + 1),
                        "version property incremented");
        }
        public void CanEvictObjectsContainedWithinAnotherSession()
        {
            Guid customerId = DbFixture.InsertCustomer();
            var  customer   = Nh.CurrentSession.Get <Customer>(customerId);

            using (ISession newSession = Nh.CreateSession())
            {
                newSession.Get <Customer>(customerId);

                //not testing yet just clarifying assumptions
                Assert.That(Nh.CurrentSession.Contains(customer), Is.True, "entity in session");

                EvictObjectsInCurrentSessionFoundWithin(newSession);
            }

            Assert.That(Nh.CurrentSession.Contains(customer),
                        Is.False,
                        "entity has been evicted from session");
        }
        public void CanRetryDeletionOfOrphanUsingAnotherSession()
        {
            //setup - trigger an exception when updating...

            Guid customerId = DbFixture.InsertCustomerWithAddress();

            var  c         = Nh.CurrentSession.Get <Customer>(customerId);
            Guid addressID = c.AddressAt(0).Id;

            c.ShortCode = "12345678"; //update will fail when saved to the db
            c.RemoveAddress(c.AddressAt(0));

            CommitSessionExpectingException();

            Assert.That(DbFixture.AddressExits(addressID), Is.True, "address not deleted");

            //run test...

            c.ShortCode = "ABCDE"; //correct problem that was causing save to fail

            PersistUsingNewSession(c);

            Assert.That(DbFixture.AddressExits(addressID), Is.False, "address is deleted");
        }
        public void CanRetryUpdateOfChild()
        {
            //setup...

            Guid id = DbFixture.InsertCustomerWithAddress();
            var  c  = Nh.CurrentSession.Get <Customer>(id);

            Address a = c.AddressAt(0);

            a.Line1 = "Another street";

            c.ShortCode = "CCCCCCCCC"; //update will fail when saved to the db

            CommitSessionExpectingException();

            //run test...

            c.ShortCode = "ABCDE"; //correct problem that was causing save to fail
            ResetVersionPropertyTo(a, 1);

            PersistUsingNewSession(c);

            Assert.That(c.ConcurrencyId, Is.EqualTo(2), "ConcurrencyId correctly incremented");
        }
        public void CanRetryUpdateWhenUsingAnotherSession()
        {
            //setup - trigger an exception when updating...

            Guid id = DbFixture.InsertCustomer();

            var c = Nh.CurrentSession.Get <Customer>(id);

            c.ShortCode = "12345678"; //update will fail when saved to the db

            int versionPropertyBeforeFlush = c.ConcurrencyId;

            CommitSessionExpectingException();

            //run test...

            c.ShortCode = "ABCDE"; //correct problem that was causing save to fail

            PersistUsingNewSession(c);

            Assert.That(c.ConcurrencyId,
                        Is.EqualTo(versionPropertyBeforeFlush + 1),
                        "customer has been updated");
        }