blogjava's web log

blogjava's web log
...

Nhibernate MultiQuery

构造多个查询。效果不错。注意Nhibernate CreateMultiQuery()这个方法
public void MultiQueryTest(int var)
{
   ISession session = null;
   session = Class1.GetFactory().OpenSession();
   IList result = session.CreateMultiQuery().Add("from Archives where id>:id")
      .Add("select count(*) from Archives a where id>:id").SetInt32("id", var).List();
   //获得第一个查询结果
   IList archives = (IList)result[0];
   long count = (long)((IList)result[1])[0];

   //当然也可以session.CreateQuery 构造查询
    //IList result = session.CreateMultiQuery()
     //    .Add(session.CreateQuery("from Archives where id>:id").SetFirstResult(5).SetMaxResults(10))
    //    .Add(session.CreateQuery("select count(*) from Archives a where id>:id"))
    //    .SetCacheable(true).SetInt32("id", var).List();
    // IList archives = (IList)result[0];
   //long count = (long)((IList)result[1])[0];

 }
 
//helper 
public class Class1
    {
        private static ISessionFactory factory;
        private static string _mappingAssembly;
 
        static Class1()
        {
 
            _mappingAssembly = ConfigurationManager.AppSettings["EntityAssemblyName"];
            NHibernate.Cfg.Configuration cfg = new NHibernate.Cfg.Configuration();
            foreach (string assembly in _mappingAssembly.Split(','))
            {
                try
                {
                    cfg.AddAssembly(assembly);
                }
                catch (MappingException me)
                {
                    throw new MappingException("Assembley Could Not Be Loaded: " + assembly, me);
                }
            }
 
            factory = cfg.BuildSessionFactory();
        }
 
        public static ISessionFactory GetFactory()
        {
            return factory;
        }
     }
更多精彩关注 http://www.ayende.com/

posted on 2007-05-12 21:18 record java and net 阅读(819) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

常用链接

留言簿(44)

新闻档案

2.动态语言

3.工具箱

9.文档教程

友情链接

搜索

最新评论