zoukankan      html  css  js  c++  java
  • 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" })))
    用对方法才有效率,做对事情才有效果
    “麻烦”是自己“处理”不当的结果
    “困难”是自己“学习”不够的反射

    “挫折”是自己“努力”不足的代价

  • 相关阅读:
    什么是OOAD?什么是UML?
    Linux下Gvim 的基本操作-适合新手看
    关于mongodb ,redis,memcache之间见不乱理还乱的关系和作用
    phpcms常用标签
    请问如何更改dedecms“文件保存目录”的字符限制 ?
    如何通过刷百度指数来提高网站的权重
    HDU 1263(水果统计 **)
    HDU 1284(钱币兑换 背包/母函数)
    HDU 1028(数字拆分 分治)
    HDU 1036(平均速度 **)
  • 原文地址:https://www.cnblogs.com/ly7454/p/2958267.html
Copyright © 2011-2022 走看看