예제 #1
0
        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);
        }
예제 #2
0
 public static void VerifyIsEquivalentToObjectInDatabase <T>(T entity, EquivalenceComparer comparer)
     where T : class
 {
     using (var session = Nh.CreateSession())
     {
         var claimFreshFromDb = session.Get <T>(Nh.GetIdOf(entity));
         AssertAreEquivalent(entity, claimFreshFromDb, comparer);
     }
 }
예제 #3
0
 protected void PersistUsingNewSession(params object[] entities)
 {
     using (ISession s = Nh.CreateSession())
     {
         foreach (object entity in entities)
         {
             s.SaveOrUpdate(entity);
         }
         s.BeginTransaction().Commit();
     }
 }
예제 #4
0
        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");
        }
예제 #5
0
        public void CanCountChildEntitiesWithoutLoadingThenIntoSession()
        {
            var     customer = Nh.CurrentSession.Get <Customer>(_customersInDb[0].Id);
            Address address  = customer.AddressAt(0);

            using (ISession newSession = Nh.CreateSession())
            {
                newSession.Get <Customer>(customer.Id);
                Assert.That(newSession.Contains(address), Is.False, "Not testing yet just clarifying assumptions");

                const string querySring = "select count(*) from Address as a where a.Customer.Id=:custId";
                newSession.CreateQuery(querySring).SetGuid("custId", customer.Id).UniqueResult();

                Assert.That(newSession.Contains(address), Is.False, "child object not loaded");
            }
        }
예제 #6
0
        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");
        }