Nhibernate笔记

 /// <summary>
 /// one to many ,IQueryOver中如何使用预加载(一次加载全部关联的)
 /// </summary>
 /// <param name="customerId"></param>
 /// <returns></returns>
 public IList<Customer> GetCustomersById(int customerId)
 {
            var p = _session.QueryOver<Customer>()
                .WhereRestrictionOn(c => c.FirstName).IsLike("ly%")
                .Fetch(c => c.Orders).Eager
                .List<Customer>();  
            return p;
 }   
        [TestMethod]
        public void TestMethodCustomer()
        {
            var c = dProvider.GetCustomersById(1);
            /*
             * 预加载由于其生成的SQL语句包括了Join或子查询语句,因此它无法保证获取到集合中元素的唯一性,例如:A包含两个子元素B和C,那么通过预加载后,
             * 第一级查询取出的列表中会包括两个A对象,而不是通常我们想象的一个。所以,启用预加载后获取到的列表,需要手动的解决唯一性的问题,
             * 最简单的就是把列表装入ISet里“过滤”一次。
             */
            var customer = new HashSet<Customer>(c).ToList();
            int count = customer.Count;

            foreach (var cu in customer)
            {
                foreach(Order od in cu.Orders)
                {
                    var o = od.OrderDate;
                }
            }
            //Assert.AreEqual(1, c.CustomerId);

        } 

 

        /// <summary>
        /// 批量 --用ISessionFactory先打开OpenStatelessSession,再用session.Insert(item); 
        /// </summary>
        /// <param name="list"></param>
        public void BatchingData(List<Customer> list)
        {
            using(var session = Sessions.Factory.OpenStatelessSession())
            using (var tx = session.BeginTransaction())
            {
                foreach (var item in list)
                {
                    session.Insert(item); 
                }
                tx.Commit();
            }
        }

 

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<Customer> GetListPage(int pageIndex, int pageSize)
        {
             using (var session = Sessions.GetSession())
             using (var tx = session.BeginTransaction())
             {
                 var p = session.QueryOver<Customer>()
                     .OrderBy(c => c.CustomerId).Asc
                     .Skip((pageIndex - 1) * pageSize)
                     .Take(pageSize)
                     .List();
                 
                 return p;
             }
        }

 

public class Sessions
    {
        private static readonly object lockObj = new object();
        private static ISessionFactory _factory;

        static Sessions() { }
        /// <summary>
        /// 这是一个典型的double lock方式,用来产生线程安全的Singletion(单例)对象。
        /// </summary>
        public static ISessionFactory Factory
        {
            get
            {
                if (_factory == null)
                {
                    lock (lockObj)
                    {
                        if (_factory == null)
                        {
                            NHibernate.Cfg.Configuration cfg = new                 NHibernate.Cfg.Configuration().Configure();
                            //cfg.AddAssembly(Assembly.GetExecutingAssembly());
                            _factory = cfg.BuildSessionFactory();
                        }
                    } // end lock
                }
                return _factory;
            }
        }
        public static ISession GetSession()
        {
            return Factory.OpenSession();
        }
    }

 

        /// <summary>
        /// 2个like的or查询
        /// </summary>
        /// <param name="fristName"></param>
        /// <param name="lastName"></param>
        /// <returns></returns>
        public static IList<Customer> GetListByWhere(string fristName, string lastName)
        {
             using (var session = Sessions.GetSession())
             using (var tx = session.BeginTransaction())
             {
                 var customer = session.QueryOver<Customer>()
                     .Where(Restrictions.On<Customer>(c => c.FirstName).IsLike("%" + fristName + "%")
                     || Restrictions.On<Customer>(s => s.LastName).IsLike("%" + lastName + "%"))
                     .List();
                 return customer;
             }
        }

其他条件的or查询

.Add(Restrictions.Or(
            Restrictions.Where<Cat>(c => c.Age > 5)
            Restrictions.On<Cat>(c => c.Name).IsIn(new string[] { "Max", "Paddy" })))

 

posted @ 2013-03-13 21:07  英雄饶命啊  阅读(188)  评论(0编辑  收藏  举报