Esempio n. 1
0
        public void MustFirstSaveReferencedObjectBeforeSavingRefereningObject()
        {
            //given
            Customer customer = Om.CreateCustomer();
            Order    order    = Om.CreateOrderWith(customer);

            //this would not happen if persistence cascaded from Order to Customer
            NhAssert.VerifyInsertThrowsBecausePropertyReferencesUnsavedInstance(order, x => x.Customer);
        }
Esempio n. 2
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");
        }