Esempio n. 1
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");
        }
Esempio n. 2
0
        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));
        }
Esempio n. 3
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);
        }