NHibernate数据库没有更新

时间:2012-02-20 06:06:07

标签: c# sql-server nhibernate nunit

namespace HRProject.Test
{
    using NUnit.Framework;
    using NHibernate;
    using HRProject.Model;
    using HRProject.Setup;
    using HRProject.Utils;
    using NHibernate.Linq;

    [TestFixture]
    public class RepositoryTest
    {
        [SetUp]
        public void SetUp()
        {
            ISession session = SessionHelper.GetSession();

            using (ITransaction tx = session.BeginTransaction())
            {
                foreach (Cashier c in session.Query<Cashier>().ToList<Cashier>())
                {
                    session.Delete(c);
                }

                tx.Commit();
            }

            using (ITransaction tx = session.BeginTransaction())
            {
                Cashier c1 = new Cashier(){Description = "D.Rathnayaka"};
                Cashier c2 = new Cashier(){Description = "K.Kumara"};
                Cashier c3 = new Cashier(){Description = "R.Gunapala"};

                session.Save(c1);
                session.Save(c2);
                session.Save(c3);

                tx.Commit();
            }
        }

        [Test]
        public void TestLoadUp() 
        { 
            ISession session = SessionHelper.GetSession();

            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                Assert.AreEqual(3, repo.Items.Count);
                Assert.AreEqual(1, (from Cashier c in repo.Items
                           where c.Description == "D.Rathnayaka"
                           select true).Count());

                tx.Commit();
            }
        }

        [Test]
        public void TestUpdate()
        {
            ISession session = SessionHelper.GetSession();

            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                Cashier cas = (from Cashier c in repo.Items
                            where c.Description == "D.Rathnayaka"
                            select c).Single();
                cas.Description = "K.R";
                session.Flush();
                tx.Commit();
            }



            using (ITransaction tx = session.BeginTransaction())
            {
                Repository<Cashier> repo = new Repository<Cashier>();
                //Fails here//
                Assert.IsNotNull((from Cashier c in repo.Items
                                  where c.Description == "K.R"
                                  select c).Single());
            }
        }
    }
}

有人可以告诉我为什么这个测试失败了吗?数据库也不会更新。存储库类如下:

namespace HRProject.Utils
{
    using NHibernate;
    using HRProject.Setup;
    using NHibernate.Linq;

    class Repository<T> where T : class
    {
        public IList<T> Items {get; set;}

        public Repository()
        {
            Items = new List<T>();

            ISession session = SessionHelper.GetSession();
            using (ITransaction tx = session.BeginTransaction())
            {
                List<T> list = session.Query<T>().ToList();

                foreach (T obj in list)
                {
                    Items.Add((T)session.Merge(obj));
                }

                tx.Commit();
            }
        }

        public void AddRepositoryObject(T obj)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道SessionHelper.GetSession方法的实现细节,但我怀疑它会在每次调用时打开新会话。如果这是真的那么测试TestUpdate有两个独立的会话对象。您在测试方法中打开一个会话,而另一个会话由Repository打开。您更新由第二个会话对象保留但在第一个会话中提交事务的实体。每个交互应该只有一个会话对象(每个会话的会话)。考虑在CurrentSessionContext对象中存储会话。