示例#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 void CanRetrySaveOfAnAggregateUsingAnotherSession()
        {
            //setup - trigger an exception when updating...

            Customer c = Om.CreateCustomer();
            Address  a = Om.CreateAddress();

            c.AddAddress(a);

            Nh.CurrentSession.SaveOrUpdate(c);

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

            CommitSessionExpectingException();

            //run test...

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

            ResetVersionPropertyTo(c, -1);
            ResetVersionPropertyTo(a, -1);
            PersistUsingNewSession(c);

            Assert.That(c.ConcurrencyId, Is.EqualTo(1), "customer has been saved");
            Assert.That(a.ConcurrencyId, Is.EqualTo(1), "address has been saved");
        }
示例#3
0
        public void CanRetrySaveOfAnEntityWithValueObjectCollection()
        {
            //setup - trigger an exception inserting objects...

            User u = Om.CreateUser();

            u.AddOtherAddress("Business", Om.CreateInvalidUserAddress());
            Nh.CurrentSession.SaveOrUpdate(u);

            CommitSessionExpectingException();

            //run test

            //fix problem that was causing save to fail
            u.RemoveOtherAddress("Business");
            u.AddOtherAddress("Business", Om.CreateUserAddress("ME5 8HU"));

            //add another address value for good measure
            u.AddOtherAddress("Other", Om.CreateUserAddress("ME5 8HU"));

            ResetVersionPropertyTo(u, -1);
            PersistUsingNewSession(u);

            Assert.That(u.ConcurrencyId, Is.EqualTo(1), "user vs property ok");
        }
示例#4
0
        public void SafeToCallDisconnectAfterImplicitDisconnect()
        {
            Nh.CurrentSession.Save(Om.CreateCustomer());
            //automatically disconnects
            Nh.CurrentSession.BeginTransaction().Commit();

            Nh.CurrentSession.Disconnect();
        }
示例#5
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);
        }
示例#6
0
        public void SaveWillSetVersionPropertyToOne()
        {
            Customer c = Om.CreateCustomer();

            Assert.That(c.ConcurrencyId, Is.Not.EqualTo(1), "not testing yet just clarifying assumptions");

            Nh.CurrentSession.Save(c);

            Assert.That(c.ConcurrencyId, Is.EqualTo(1), "version property incremented");
        }
示例#7
0
        public void DeleteCancelsSave()
        {
            //setup
            Customer customer = Om.CreateCustomer();

            Nh.CurrentSession.Save(customer);

            Nh.CurrentSession.Delete(customer);
            Assert.That(Nh.CurrentSession.Get <Customer>(customer.Id), Is.Null);
        }
示例#8
0
        public void Save_WhenEntityReferencesTransientInstance_WillThrowImmediately()
        {
            Customer c = Om.CreateCustomer();
            Order    o = Om.CreateOrder();

            //Order now has a reference to a transient (unsaved) Customer
            o.Customer = c;
            var ex = Assert.Throws <PropertyValueException>(() => Nh.CurrentSession.Save(o));

            Assert.That(ex.Message, Is.StringContaining("not-null property references a null or transient value"));
        }
示例#9
0
        public void TransactionNotMarkedAsCommittedIfCommitFails()
        {
            Customer c = Om.CreateInvalidCustomer();

            Nh.CurrentSession.SaveOrUpdate(c);
            ITransaction tx = Nh.CurrentSession.BeginTransaction();

            NUnitTestsBase.ExecuteAndExpect <Exception>(tx.Commit);

            Assert.That(Nh.CurrentSession.Transaction.WasCommitted, Is.False);
        }
示例#10
0
        public void FlushWillPersistChangesMadeAfterCallingSaveAsASeperateUpdate()
        {
            Customer customer = Om.CreateCustomer();

            Nh.CurrentSession.Save(customer);

            customer.Name = "New name";
            Nh.FlushSessionToDbAndClear();

            Assert.That(customer.Name, Is.EqualTo(Nh.GetFromDb <Customer>(customer.Id).Name));
            Assert.That(customer.ConcurrencyId, Is.EqualTo(2));
        }
示例#11
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");
        }
示例#12
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);
        }
示例#13
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));
        }
示例#14
0
        public void SaveWillMakeObjectVisibleToGet()
        {
            //setup
            Customer customer = Om.CreateCustomer();

            Nh.CurrentSession.Save(customer);

            //run test...
            var customerFromSession = Nh.CurrentSession.Get <Customer>(customer.Id);

            Assert.That(customerFromSession, Is.Not.Null, "object expected to be visible to Get");
            Assert.That(customerFromSession, Is.SameAs(customer), "Get expected to return the object passed to Save");
        }
示例#15
0
        public void DisposingOfTransactionWillRollbackTransactionIfNotCommitted()
        {
            Customer c = Om.CreateCustomer();

            using (Nh.CurrentSession.BeginTransaction())
            {
                Nh.CurrentSession.SaveOrUpdate(c);
                Nh.FlushSessionToDbAndClear();
            }

            Assert.That(Nh.CurrentSession.Get <Customer>(c.Id),
                        Is.Null,
                        "Customer not found in database");
        }
示例#16
0
        public void WillNotAutomaticallyReconnectAfterExplicitCallToDisconnect()
        {
            //setup
            Customer customer = Om.CreateCustomer();

            Nh.CurrentSession.Save(customer);
            Nh.CurrentSession.Flush();

            Nh.CurrentSession.Clear();
            Nh.CurrentSession.Disconnect();

            //run test
            Exception ex = Assert.Throws <HibernateException>(() => Nh.CurrentSession.Get <Customer>(customer.Id));

            Assert.That(ex.Message, Is.EqualTo("Session is currently disconnected"));
        }
示例#17
0
        public void FlushWillReachNewObjectsAttachedToAPersistenceInstanceAfterSave()
        {
            Customer c = Om.CreateCustomer();

            Nh.CurrentSession.SaveOrUpdate(c);

            //note that customer already associated with session
            Address a = Om.CreateAddress();

            c.AddAddress(a);

            Nh.FlushSessionToDbAndClear();

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

            Assert.That(loadedCustomer.Addresses.Count, Is.EqualTo(1));
        }
示例#18
0
        public void NHibernateCriteriaSpikesSetup()
        {
            Customer c1 = Om.CreateCustomerWithOneAddress();
            Customer c2 = Om.CreateCustomerWithOneAddress();

            c2.AddAddress(Om.CreateAddress("ME5 8HU"));
            Customer c3 = Om.CreateCustomer();

            c3.Name = "Mervyn Ramos";
            Customer c4 = Om.CreateCustomer();

            c4.ShortCode = "009";

            _customersInDb = new List <Customer>();
            _customersInDb.AddRange(new[] { c1, c2, c3, c4 });

            Nh.InsertIntoDb(_customersInDb);
        }
示例#19
0
        public void CanRetrySaveUsingAnotherSession()
        {
            //setup - trigger an exception when saving...

            Customer c = Om.CreateInvalidCustomer();

            Nh.CurrentSession.Save(c);

            CommitSessionExpectingException();

            //run test...

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

            PersistUsingNewSession(c);

            Assert.That(c.ConcurrencyId, Is.EqualTo(1), "customer has been saved");
        }
示例#20
0
        public void FlushInsideATransactionWillNotBeCommittedUnlessTransactionCommitted()
        {
            Customer c = Om.CreateCustomer();

            using (ITransaction transaction = Nh.CurrentSession.BeginTransaction())
            {
                Nh.CurrentSession.SaveOrUpdate(c);
                Nh.FlushSessionToDbAndClear();
                Assert.That(Nh.CurrentSession.Get <Customer>(c.Id),
                            Is.Not.Null,
                            "Customer saved in database");
                transaction.Rollback();
            }

            Nh.CurrentSession.Clear();
            Assert.That(Nh.CurrentSession.Get <Customer>(c.Id),
                        Is.Null,
                        "Customer not found in database");
        }
示例#21
0
 public static Guid InsertCustomerWithAddress()
 {
     return(Insert(Om.CreateCustomerWithOneAddress()));
 }
示例#22
0
 public static Guid InsertCustomer()
 {
     return(Insert(Om.CreateCustomer()));
 }
示例#23
0
        public void DeleteDoesNotRequireAnOpenConnection()
        {
            Nh.CurrentSession.Disconnect();

            Nh.CurrentSession.Delete(Om.CreateCustomer());
        }